File open method

File object methods:

File object methods to perform operations

f.close () closes the file

f.read ([size = -1]) read characters from the document size, when the size is not given or given negative, all remaining characters to read, then returned as a string

f.readline ([size = -1]) read from the file and return line (including the line terminator) if size is defined size characters returns

f.write (str) will be written to the file string str

f.writelines (seq) is written to the file string sequence seq, seq should return a string iterable

f.seek (offset, from) moving the file pointer in the file, from from (0 represents the file start position, represents the current position, represents the end of the file) byte offset offset

f.tell () returns the current position in the file

f.truncate ([size = file.tell ()]) taken to the file size bytes, the default is the current file pointer position to intercept

1. python support the file operation, the file is opened using the Open (), and set the open mode.

2. Open the file you want to close, after closing to the contents of the cache written to disk, not lost

01. f = open ( 'D: \\ python \\ python_study \\ write.txt') # default open mode 'rt'

02. f.close () # close

3. Open three kinds of files:

01. f = open('E:/test.txt', 'w')  

02. f = open('E://test.txt', 'w')

03. f = open('E:\\test.txt', 'w')

4. Print file object iteration (f) each row of data:

01. for each_line in f:

02.  print(each_line)

5. In the document, a word is two bytes, f.tell () returns the position of the pointer file

6. At the time of writing, it is preferable to use the open mode 'x'

7. About file extensions:

01. Long-term use of windows operating system extensions friends can easily be misled by that file type extension decision, in fact, this concept is wrong!

02. This is in fact a surname with the bad guys, even though the name changed to "Zhang good man," but he was a bad guy is a reason ^ _ ^

03. extension on the file, easy for beginners into the errors:

Myth: The file extension is a necessary component of a file:

With or without a file extension, for the open file operation, no need to select the file name extension program to open it with the file extension is automatically set up with a good program (if any) to try to open (is "trying to open "rather than" open reason "see the first of the following two errors), the file extension is a component of a regular file, but a file does not necessarily need an extension.

Myth: The file extension watches clear what type of file:

File extension can be set manually, TXT file extension is likely to be a picture, too, file extension M-P3, still might be a video.

Guess you like

Origin www.cnblogs.com/haozong/p/11238270.html
Recommended