File and directory operations:
-File read and write :The function of Python for file reading and writing is open or
File_handler =
Open (Filename,mode)
Mode
Mode |
Description |
R |
Read-only |
r+ |
Write |
W |
Write, delete the original file before re-writing, if the file is not created |
w+ |
Read and write, first delete the original file, re-write, if the file is not created (can write output) |
Read Example:
>>> fo = open ('/tools/123/test.log ') #open函数打开文件 >>> fo<open file '/tools/123/test.log ', mode ' R ' at 0x7fdc0c144d20> #fo对象打开的文件, Mode is Read>>> fo.read () ' a\n ' #查看文件中的数据 >>> fo, Close () #关闭文件 >>> fo.read () traceback (most recent call last): File "<stdin>", line 1, in <module>valueerror: i/o operation on closed file # Read file again, is off state ################################ #利用file类打开文件 ##################################>>> f1 = file ('/tools/123/test.log ') >>> f1.read () ' a\n ' >>> f1.close () >> > f1.read () Traceback (most recent call last): file "<stdin>", line 1, in <module>valueerror: i/o operation on closed file# Method Ibid.
Write Example:
>>> fnew = open ('/tools/123/new.test ', ' W ') #打开新的文件, note the quotation marks of W, in the form of a worthy pass # here the file new.test already exists >>>fo.write (' Hello \n my name is kong ') #写入数据, note that,\n wraps in the buffer >>> fo.close () #关闭, You can save the data >>> fnew=open ('/tools/123/new.log ', ' W ') #注意w的特性, When it opens the file, it empties the original data; >>> fnew.write (' i am new ') #再次写入数据 >>> Fnew.close ()
Open file with r+:(write data after viewing)
>>> fnew=open ('/tools/123/new.log ', ' r+ ') >>> fnew.read () #文件可读 ' I am new ' >>> fnew.write (' new contents ') >>> fnew.close () #查看文件保留原有数据, and write new data
(write data directly)
>>> fnew=open ('/tools/123/new.log ', ' r+ ') >>> fnew.write (' Nihao ') >>> fnew.close () #查看文件内容
#文件中的数据, when we go to check it, it will print from the first letter to the last letter, in fact, it has an invisible pointer, starting from the first character to read the time
No learning is completed here ~ ~ ~ Note
OS module :
Import OS
-Directory operation requires calling OS module
-for example :
>>> Import os >>> os.mkdir ('/TOOLS/CSVT ') >>> os.mkdir (' test ')
Multilevel Directory Creation makedirs ():
>>> os.makedirs (' a/b/c ')
Delete directory rmdir (), Removedirs ():
>>> os.rmdir (' test ') >>> os.rmdir (' a ') Traceback (most recent call last): File "<stdin>", line 1, I n <module>oserror: [Errno] Directory not empty: ' A ' #提示目录不为空 >>> os.removedirs (' a/b/c ') # Note the deletion of multilevel directories to indicate the directory list
Show current path getcwd ():
>>> os.getcwd () '/tools/123 '
Show directory list listdir () :
>>> os.listdir ('. ') [' Test.log ', ' new.log ', ' new.test ']#. Current path, but does not contain subdirectories >>> os.listdir ('/tools/123/') [' Test.log ', ' new.log ', ' New.test ']
Switch directory operation chdir:
>>> os.chdir ('/usr/local/') >>> os.getcwd () '/usr/local ' #注意此切换仅仅是在python environment
File Object operations:
-fileobject.close ()
-string = Fileobject.readline ([size])
-list = Fileobject.readlines ([size])
-string = Fileobject.read ([size])
-fileobject.next ()
-fileobject.write (String)
-fileobject.writelines (list)
-fileobject.seek (offset, option)
-fileobject.flush ()
Example:
>>> for I in open (' Test.log '): ... print I ... www.csvt.com i am milohello
#遍历test. Log, printing each line sequentially, returning the iteration variable
ReadLine
-Format
String=fileobject.readline ("size")
Description
Each time a row of the file is read
Size: Refers to each line reading a size byte, knowing the end of the line
Example:
>>> f1 = open (' Test.log ') >>> f1.readline () ' www.csvt.com\n ' >>> f1.readline () ' I am milo\n ' >>> f1.readline () ' hello\n ' >>> f1.readline () '
#每次读取一行 until the correction is complete.
ReadLines
>>> f1.close () >>> f1 = open (' Test.log ') >>> f1.readlines () [' www.csvt.com\n ', ' I am milo\n ', ' Hello\n ']
#把每行的数据作为列表当中的元素进行返回
Next
Format
Fileobject.next ()
Description
. Returns the current row, and the file pointer to the next line
Example:
>>> f1.close () >>> f1=open (' Test.log ') >>> f1.next () ' www.csvt.com\n ' >>> f1.next ( ) ' I am milo\n ' >>> f1.next () ' hello\n ' >>> f1.next () Traceback (most recent call last): File "<stdin> ; ", line 1, in <module>stopiteration
#效果和readline类似, note that with ReadLine, an empty string is read after the range is exceeded, and next, after the range, stops iterating
Write :
Format
. Fileobject.write (String)
Description
. Write and subsequent writelines whether to erase the original data in the file before writing, and to re-write the new content, depending on the mode of the open file ;
Example:
Writelines:
Format
. Fileobject.writelines (List)
Description
. Multi-line Write
Efficiency is higher than write, faster, and a small number of writes can be used write
Example:
>>> L = [' one\n ', ' two\n ', ' three\n ']>>> f1 = open (' Test.log ', ' a ') >>> F1.writelines (L) > >> F1.close ()
Fileobject.seek (offset, option)
-When =o, indicates that the file pointer is pointing from the file header to the "offset" byte.
-option = 1 o'clock, which indicates that the file pointer points to the current position of the file and moves the "offset" byte backwards.
-option = 2 o'clock, which means pointing the file pointer from the end of the file, pointing to the move offset byte.
Need to re-learn
Python files and directories do not work well and need to be re-learned