python - file 文件操作

The first thing you’ll need to do is use the built-in python open file function to get a file object

When you use the open function, it returns something called a file objectFile objects contain methods and attributes that can be used to collect information about the file you opened. They can also be used to manipulate said file.

For example, the mode attribute of a file object tells you which mode a file was opened in. And the name attribute tells you the name of the file.

You must understand that a file and file object are two wholly separate – yet related – things.

 

A file In Python is categorized as either text or binary, and the difference between the two file types is important.

Text files are structured as a sequence of lines, where each line includes a sequence of characters. This is what you know as code or syntax.

 

Each line is terminated with a special character, called the EOL or End of Line character. There are several types, but the most common is the comma {,} or newline character. It ends the current line and tells the interpreter a new one has begun.

A backslash character can also be used, and it tells the interpreter that the next character – following the slash – should be treated as a new line. This character is useful when you don’t want to start a new line in the text itself but in the code.

A binary file is any type of file that is not a text file. Because of their nature, binary files can only be processed by an application that know or understand the file’s structure. In other words, they must be applications that can read and interpret binary.

 

 

 

Reading Files in Python

In Python, files are read using the open() method. This is one of Python’s built-in methods, made for opening files. 

The open() function takes two arguments: a filename and a file opening mode. The filename points to the path of the file on your computer, while the file opening mode is used to tell the open() function how we plan to interact with the file.

By default, the file opening mode is set to read-only, meaning we’ll only have permission to open and examine the contents of the file.

 

 

 

 

 

 

 

 

 

 

 

 

Files are named locations on disk to store related information. They are used to permanently store data in a non-volatile memory (e.g. hard disk).

Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned off), we use files for future use of the data by permanently storing them.

When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed so that the resources that are tied with the file are freed.

 

方法1:

 

 

 

 

 

 

 

 

方法2:

 

 

 

方法3:

 

 

with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Unlike the above implementations, there is no need to call file.close() when using with statement. The with statement itself ensures proper acquisition and release of resources

 

 

 

 

We can read a file line-by-line using a for loop. This is both efficient and fast.

In this program, the lines in the file itself include a newline character \n. So, we use the end parameter of the print() function to avoid two newlines when printing.

 

 

 

Alternatively, we can use the readline() method to read individual lines of a file. This method reads a file till the newline, including the newline character.

 

 

 

Lastly, the readlines() method returns a list of remaining lines of the entire file. All these reading methods return empty values when the end of file (EOF) is reached.

 

 

 

 

 

 

 

 

write 写文件:

 

 

 

 

 

 

 

 

 

 

 

 

csv file

 

 

 

 

posted @ 2021-10-07 11:49  xman888  阅读(110)  评论(0编辑  收藏  举报