#encoding =utf-8#author:zhouyu
Os This module is used for some basic operations of system files, comparing the creation of files, modifying files, and deleting files.
In general, if you want to get the contents of this file or if you want to modify the file information, you must first open the file.
import os #导入os模块test_file = ' c:\\test.txt ' opentest = open (Test_file, ' r ') #以只读的形式打开文件read_test = Opentest.read () #把文件 Content stored in opentest variable print (read_test)
No matter what file you read, Python reads a cursor that is used to record where you read a virtual body,
The next time you come to read the file, it will start reading from this cursor, unless you close the file and reopen the file,
Or you can put this cursor where you want to start reading, because we've finished reading this file, and you're reading it now.
You can't read it because the cursor is at the end.
Print ("Now I want to continue read above file") Read_test1 = Opentest.read () print (READ_TEST1)
Now we can see where this cursor is.
Print (Opentest.tell ())
The printed result is 91, which means that the cursor is in the 91st character there, and if you want to re-read this file,
You can adjust the cursor with the following command
Print (Opentest.seek (0))
Let's read this file and give the content to the READ_TEST1 variable.
Read_test1 = Opentest.read ()
And then you turn your cursor to 0, and we'll print the files we read.
Print (READ_TEST1)
Python is rather stupid, it says read-only, it can't write files, if you put the following code to remove the error will be
Opentest.write ("This is Test")
Remember to close the file every time you run out of files. The following code closes the file
Opentest.close ()
The following is a write operation, but generally not recommend this, because you use W words python will create a file, if this file
exists, it will overwrite the file, which means the contents of the file will be emptied, which is very dangerous.
opentest = open (Test_file, ' W ') opentest.write ("You'll found your file ' content is Clear,and rewrite this!!!") Opentest.close ()
If you don't want to, just want to add content at the end of the file, you can use the following methods:
opentest = open (Test_file, ' a ') opentest.write ("\nthis is APPEND statement!!!") Opentest.close ()
When you're done with this, you can go and see if there's anything to add to your file.
If I just want to see what happens in the first 5 lines, there are two ways to do it, but your file must have 5 lines.
The following is the most efficient
Count = 0test_file = ' c:\\test.txt ' opentest = open (Test_file, ' r ') for I in Opentest:if count <= 5:count =+1 Print (i) opentest.close ()
The following is a relatively inefficient
Count = 0test_file = ' c:\\test.txt ' opentest = open (Test_file, ' r ') for I in range (5): If Count <= 5:count =+1 Print (Opentest.readline ()) Opentest.close ()
Why do they have high and low efficiency, because the first is to use iterators, each time only in the file to take
One piece of data, and the second is to remove all the contents of the file, such as
If your file has 20G, then you will have to wait a long time, and your memory is not very likely to put 20G, so
The first efficient method is generally used
This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1965087
Basic operations for Python files