in Python, the OS module and Shutil module used frequently in the file and directory operation are often use.
1. Get the current working folder, that is, the folder path of the current Python script work: OS.GETCWD ()
2. Returns all file and folder names under the specified folder name: Os.listdir ()
3. function to delete a file: Os.remove ()
4. Delete multiple folders: Os.removedirs (r "C:\python")
5. Verify that the given path is a file: Os.path.isfile ()
6. Verify that the given path is a folder: Os.path.isdir ()
7. Infer if it is an absolute path: Os.path.isabs ()
8. Verify that the given path is really saved: os.path.exists ()
9. Returns the folder name and file name of a path: Os.path.split () eg os.path.split ('/home/swaroop/byte/code/poem.txt ') Result: ('/home/swaroop/byte/ Code ', ' Poem.txt ')
10. Detach extension: Os.path.splitext ()
11. Get Path name: Os.path.dirname ()
12. Get File Name: Os.path.basename ()
13. Execute shell command: Os.system ()
14. Read and environment variable settings: os.getenv () and os.putenv ()
15. Give the line terminator used by the current platform: os.linesep Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '
16. Indicate the platform you are using: Os.name for Windows, it is ' NT ', and for Linux/unix users. It's ' POSIX '
17. Rename: Os.rename (old. New
18. Create a Multilevel folder: Os.makedirs (r "C:\python\test")
19. Create a single folder: Os.mkdir ("Test")
20. Get File attributes: Os.stat (file)
21. Change file permissions and timestamps: Os.chmod (file)
22. Terminate the current process: Os.exit ()
23. Get File Size: os.path.getsize (filename)
Second, the file operation method Daquan:
1.os.mknod ("Test.txt") #创建空文件
2.FP = open ("Test.txt", W) #直接打开一个文件, create a file if the file does not exist
3. About open mode:
Copy CodeCode such as the following: w: Open in writing,
A: Open in Append mode (start with EOF and create new file if necessary)
r+: Open in read-write mode
w+: Open in read/write mode (see W)
A +: Open in read/write mode (see a)
RB: Open in binary read mode
WB: Open in binary write mode (see W)
AB: Open in binary append mode (see a)
rb+: Open in binary read/write mode (see r+)
wb+: Open in binary read/write mode (see w+)
ab+: Open in binary read/write mode (see A +)
Fp.read ([size]) #size为读取的长度. In units of byte
Fp.readline ([size]) #读一行, assuming that size is defined, it is possible to return only part of a row
Fp.readlines ([size]) #把文件每一行作为一个list的一个成员. and returns the list. In fact its interior is implemented by looping through calls to ReadLine (). Suppose you provide a size parameter. The size is the total length of the read content, which means that only a portion of the file may be read.
Fp.write (str) #把str写到文件里, write () does not add a newline character after Str
Fp.writelines (seq) #把seq的内容所有写到文件里 (multi-line write-once). This function is simply written faithfully, and does not add anything after each line.
Fp.close () #关闭文件. Python will voluntarily close the file after a file is not used. Only this feature is not guaranteed. You might as well develop your own closed habits. Suppose a file is closed and then manipulated to produce valueerror
Fp.flush () #把缓冲区的内容写入硬盘
Fp.fileno () #返回一个长整型的 "file label"
Fp.isatty () #文件是否是一个终端设备文件 (on UNIX systems)
Fp.tell () #返回文件操作标记的当前位置. Start with the origin of the file
Fp.next () #返回下一行. and shifts the file action marker to the next line. Use a file for the for ... in file such a statement. is to invoke the next () function to implement the traversal.
Fp.seek (Offset[,whence]) #将文件打操作标记移到offset的位置.
This offset is usually calculated relative to the beginning of the file. Generally a positive number. But assuming that whence is provided, whence is able to calculate from the beginning of the 0. 1 indicates that the current position is the origin of the calculation. 2 means that the end of the file is calculated as the origin. It is important to note that the file is opened in a or a + mode. Each time the write operation is performed. The file action tag will voluntarily return to the end of the file itself.
Fp.truncate ([size]) #把文件裁成规定的大小. The default is where the action tag is cropped to the current file. Assume that size is larger than the file, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.
Three, folder operation Method Daquan
1. Create a folder
Os.mkdir ("file")
2. Copy the file:
Shutil.copyfile ("Oldfile", "NewFile") #oldfile和newfile都仅仅能是文件
Shutil.copy ("Oldfile", "NewFile") #oldfile仅仅能是文件夹. NewFile can be a file. can also be a destination folder
3. Copy the directory:
4.shutil.copytree ("Olddir", "Newdir") #olddir和newdir都仅仅能是文件夹, and newdir must not exist
5. Renaming files (folders)
Os.rename ("Oldname", "NewName") #文件或文件夹都是使用这条命令
6. Move files (folders)
Shutil.move ("Oldpos", "Newpos")
7. deleting files
Os.remove ("file")
8. Delete a folder
Os.rmdir ("dir") #仅仅能删除空文件夹
Shutil.rmtree ("dir") #空文件夹, contents of the folder can be deleted
9. Convert folders
Os.chdir ("path") #换路径
Python file operations