1. Basic file operation
- Open () Opens or creates a file
Format: Open (' file path ',' open mode ') return value: File IO object open mode altogether n kinds: W-mode write mode when the write file does not exist when the file is created, if the file already exists it will empty the file R mode Reading mode read file does not exist on the error, there is ready to read the file a mode append mode append File does not exist then new, file exists then append to the end of file x mode or schema file exists error, file does not exist then new file B mode binary mode binary auxiliary mode cannot use + mode enhanced mode Plus also auxiliary mode cannot be used alone
format: File IO object. Close () return value: None # Create a new file with the file name: test.txtf = open ('test.txt'w') # close this file
Format: File IO object. Read () return value: The character format of the entire file: File IO object. read (character length) return value: A character of the specified length
- ReadLine () reads a row of files
format: File IO object. ReadLine () return value: String format for a single line of content: File IO object. readline (character length) return value: string of one line note: Character length < current inline content Reads a string of the specified length, and the next time it reads or gets the content that is not read in this row. Character length >= the current line content, directly read the forward
- ReadLines () reads the contents of the file into the sequence.
Format: File IO object. ReadLines () return value: List format: File IO object. readlines (character length) return value: List Note: The number of rows read is determined by the length of the character, and if the character length reads n rows, there are also characters of the specified length without reading, the next line is read directly in
Format: File IO object. Write (string: str) return value: The length of the write string
- Writelines () writes the sequence to the file
Format: File IO object. writelines (sequence: list) return value: None
2. OS module file and folder operation
Functions in the OS module:
Serial Number |
Function Name |
Description |
format |
1 |
GETCWD () |
Get the current working directory |
Format: OS.GETCWD () Return value: Path string |
2 |
ChDir () |
Modify the current working directory |
Format: Os.chdir () return value: None |
3 |
Listdir () |
Gets the specified folder in the List of all files and folders |
Format: Os.listdir (directory path) Return value: List of content names in the directory |
4 |
mkdir () |
Create a Directory/folder |
Format: Os.mkdir (directory path) return value: None |
5 |
Makedirs () |
To create a folder recursively |
Format: os.makedirs (PATH) |
6 |
RmDir () |
Remove a directory (must be an empty directory) |
Format: Os.rmdir (directory path) return value: None |
7 |
Removedirs () |
To delete a folder recursively |
Format: Os.removedirs (directory path) return value: None Note the lowest-level directory must be empty |
8 |
Rename () |
Modify the names of files and folders |
Format: os.rename (source file or folder, destination file or folder) return value: None |
9 |
Stat () |
Get information about a file |
Format: Os.stat (file path) Return value: A tuple containing file information |
10 |
System () |
Execute system command |
Format: Os.system () Return value: Integral type Use with caution! You're going to have fun with a RM-RF! |
11 |
Getenv () |
Get System Environment variables |
Format: os.getenv (Gets the environment variable name) return value: String |
12 |
Putenv () |
Setting System Environment variables |
Format: os.putenv (' environment variable name ', value) return value: None Note: The getenv is not detected properly. |
13 |
Exit () |
Launches the current execution command to directly close the current operation |
Format: Exit () return value: None |
3, OS. Path module
OS. Path Module Function section:
Serial Number |
Function Name |
Description |
format |
1 |
Abspath () |
Convert a relative path to an absolute path |
Format: Os.path.abspath (relative path) return value: Absolute path string |
2 |
BaseName () |
Get the folder or file name in the path (As long as the last part of the path) |
Format: os.path.basename (PATH) Return value: The last part of the path (possibly a file name or a folder name) |
3 |
DirName () |
Gets the path part of the path (out of the last part) |
Format: os.path.dirname (PATH) Return value: The content string in the path except the last part |
4 |
Join () |
Synthesize 2 paths into one path |
Format: Os.path.join (path 1, path 2) Return value: The path after merging |
5 |
Split () |
Cut a path into the folder and filename section |
Format: os.path.split (PATH) return value: Tuple |
6 |
Splitext () |
Cut a file name into a two-part name and suffix |
Format: Os.path.splitext (file name) return value: Tuple (name, suffix) |
7 |
GetSize () |
Get the size of a file |
Format: os.path.getsize (PATH) return value: Integer |
8 |
Isfile () |
Detects if a path is a file |
Format: os.path.isfile (PATH) Return Value: Boolean value |
9 |
Isdir () |
Detects if a path is a folder |
Format: Os.path.isdir (PATH) Return Value: Boolean value |
10 |
Getctime () |
Gets the creation time of the file! Get Create Time |
Format: Os.path.getctime (file path) Return value: Time stamp floating point number |
11 |
Getmtime () |
Get the file modification time! Get Modify Time |
Format: Os.path.getmtime (file path) Return value: Time stamp floating point number |
12 |
Getatime () |
Get the file access time! Get active Time |
Format: Os.path.getatime (file path) Return value: Time stamp floating point number |
13 |
Exists () |
Detects if the specified path exists |
Format: os.path.exists (PATH) Return Value: Boolean value |
14 |
Isabs () |
Detects if a path is an absolute path |
Format: os.path.isabs (PATH) Return Value: Boolean value |
15 |
Islink () |
Detects if a path is a link |
Format: Os.path.islink (PATH) Return Value: Boolean value |
16 |
Samefile () |
Detects if 2 paths point to the same file |
Format: Os.path.samefile (path 1, path 2) Return Value: Boolean value |
Python Development "section 6th", "File Operations"