Get the current working directory, that is, the directory path of the current Python script work: OS.GETCWD ()
Returns all files and directory names under the specified directory name: Os.listdir ()
function to delete a file: Os.remove ()
Delete multiple directories: Osremovedirs (r "C:\python")//slightly dangerous, skilled, then use it.
Verify that the given path is a file: Os.path.isfile ()//often uses
Verify that the given path is a directory: Os.path.isdir ()//often uses
Determine if it is an absolute path: Os.path.isabs ()
Verify that the given path is true: Os.path.exists ()
Returns the directory name and file name of a path: Os.path.split ()
Example: Import OS
Os.path.split ('/home/swaroop/byte/code/poem.txt ')
The result is: ('/home/swaroop/byte/code ', ' poem.txt ')//It is more clear that the path and file name are listed separately
Detach extension: Os.path.splitext ()
Get path name: Os.path.dirname ()
Get file name: Os.path.basename ()
Run shell command: Os.system ()
Read and SET Environment variables: os.getenv () and os.putenv ()
Gives the line terminator used by the current platform: os.linesep Windows uses ' \ r \ n ', Linux uses ' \ n ' and mountainlion uses ' \ r '
Show the platform you are using: Os.name for Windows, he is ' NT ', and for Linux/unix, he is ' POSIX '
Renamed: Os.rename (old,new)
Create a multi-set catalog: Os.makedirs (r "C:\python\test")
Create a single directory: Os.mkdir ("Test")
Get file attributes Os.stat (files)
Modify file permissions and timestamps: Os.chmod (file)
Terminate current process: Os.exit ()//python2.4 available
Get File Size: os.path.getsize (filename)
File operation:
Os.mknod ("test.txt") Create an empty file
fp = open ("Test.txt", W) opens a file directly and creates a file if the file does not exist
About the Open/file mode:
W Open in a written way
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode//It is said to not be useful
A + read/write mode open//I prefer to use, read and write open and append
RB opens in binary read mode
WB opens in binary write mode
AB opens in binary append mode
rb+ Open in binary read-write mode
wb+ Open in binary read-write mode
ab+ Open in binary read-write mode
Fp.read ([size])//size is read length, in bytes
Fp.readline ([size])//read a line, if a size is defined, it is possible to return only part of a row
Fp.write (str)//write str to a file, write () does not add a newline character after Str
Fp.writelines (SEQ)//Writes the contents of the SEQ to the file (multiline write-once). This function is simply written faithfully and will not add anything behind each line.
Fp.close ()
Fp.flush ()//write the contents of the buffer to the hard disk
Fp.fileno ()//Returns a long-shaped "file label"
Fp.isatty ()//file is a terminal device file (Unix system)
Fp.tell ()//return to the current position, e.g.:
fp = open ("Zhige.txt", ' A + ') content in//zhige.txt is Zhigedahaoren
Fp.read (3)
c = Fp.tell ()
Print c//will return to display the third letter in Zhigedahaoren I
Fp.next ()//returns to the next line and moves the file action marker bit to the next line
When a file is used for a statement such as for...in file, the next () function is called to implement the traversal
Fp.seek (Offset[,whence])//file cursor moved to offset position and tell match
It's more obvious to do experiments.
Fp.truncate ([size])//The file is cut to the specified size, the default is the location of the current file operation coordinates. If size is larger than the file, depending on the system, the file may not change, it may be 0 files to the appropriate size, or it may be added with some random content.
Directory operation://feel can be done with Os.system (") to write the shell
Os.mkdir ("file") Create directory
To copy a file:
Shutil.copyfile ("Oldfile", "NewFile")//oldfile and NewFile are all smart files
Shutil.copy ("Oldfile", "NewFile")//oldfile can only be files, NewFile can be
Either a file or a target directory
To copy a folder:
Shutil.copytree ("Olddir", "Newdir")//olddir and Newdir can only be directories, and newdir must not exist
Rename file (directory):
Os.rename ("Oldname", "newname")//file or directory is this command
Moving Files (directories)
Shutil.move ("Oldpos", "Newpos")
deleting files
Os.remove ("file")
To delete a directory:
Os.rmdir ("dir")//can only delete empty directory
Shutil.rmtree ("dir")//Empty directory, the contents of the directory can be deleted
To convert a directory:
Os.chdir ("path")//Replace Path
Some explanations:
Seek (Offset,where): Where=0 moves from the starting position, 1 moves from the current position, 2
Moves from the end position. When there is a newline, it is truncated by line breaks. Seek () has no return value, so the value is none.
Tell (): The current position of the file, where tell is the location where the file pointer is obtained, subject to
Seek,readline,read,readlines influence, unaffected by truncate
Truncate (n): Truncates from the first line character of the file, truncates the file to n characters, no n indicates truncation from the current position, and the word after n after the stage is deleted. Where Win's newline represents a 2-character size.
ReadLine (N): reads in several rows, n indicates the maximum number of bytes to read in. Where the read start position is tell () +1. When n is empty, the contents of the current row are read-only by default
ReadLines read in all line contents
Read reads all line contents
Second, following an example to illustrate the function of the above functions
FSO = open ("F:\\a.txt", ' w+ ')//In w+ way, not a way to open the file, so the original content is emptied
Print Fso.tell ()//The original contents of the file are emptied, so tell () =0
Fso.write ("abcde\n")//write to file abcde\n, because newline \ n is 2 characters, so 7 characters are written
Print Fso.tell () this time tell () =7
Fso.write ("FGHWM")///write file Fghwm, so the file is written 7+5=12 characters
Print Fso.tell ()//At this time tell () =12
Fso.seek (1,0)//start by moving a character from the first line of the file
Print Fso.tell ()//At this time tell () =1
Print fso.readline ()//reads the current line, which is the first line of the file, but reads from the second character, resulting in BCDE
To read the entire file or read the entire file, the result is BCDEFGHWM
Print Fso.tell ()//Because ReadLine now tell () =7
Fso.truncate (8)//starts with the first line character of the file after writing, truncates to 8 characters, i.e.
ABCED\NF, that is, the file content is: ABCDE\NF
Print Fso.tell ()//tell () is still 7 and is affected by truncate (8), but the contents of this file are ABCDE\NF
Print fso.readline ()//starts reading from tell () +1=8, reads the current line contents: F
Transferred from: http://blog.sina.com.cn/s/blog_86bf6b700101nani.html
How Python is often used in automating operations