The need for file processing in Python involves OS modules and Shutil modules
Get current working directory path: OS.GETCWD ()
Gets all files and directory names under the specified directory name: Os.listdir (dir)
Delete files: os.remove (file)
Delete multiple directories: Os.removedirs (r "/home")
Detect if path is file: Os.path.isfile (PATH)
Detect if Path is directory: Os.path.isdir (PATH)
Determine if absolute path: os.path.isabs (PATH)
Detects if a path exists: os.path.exists (PATH)
Returns the directory name and file name of a path: os.path.split (PATH)
Detach extension: Os.path.splitext (file)
Get path name: Os.path.dirname (file)
Get file Name: Os.path.basename (file)
Run shell command: Os.system
Read and SET Environment variables: os.getenv () and os.putenv ()
Gives the line terminator for the current platform: os.linesep (Windows is \r\n,linux for \N,MAC \ R)
Indicates the platform you are using: Os.name (Windows is nt,linux for POSIX)
Renamed: Os.rename (old,new)
Create a multilevel directory: Os.makedirs (r "C:\python\test")
Create a single directory: Os.mkdir ("Test")
Get file attributes: Os.stat (file)
Modify file permissions and timestamps: Os.chmod (file)
Terminate current process: Os.exit ()
Get File Size: os.path.getsize (filename)
File operation:
File content Substitution
For line in Fileinput.input ("filepath", inplace=1):
line = Line.replace ("OldText", "NewText")
Print line,
Os.mknod ("Text.txt") creates an empty file
fp = open ("Text.txt", ' W ') directly opens a file and creates a file
about open mode if it does not exist:
W Open as write (if the file exists to empty the contents of the file)
a opens in Append mode (starting with EOF, creating a new file if necessary)
r+ open in read-write mode
w+ open in read-write mode (see W)
A + opens in read-write mode (see a)
RB opens in binary mode
WB opens in binary mode (see W)
AB opens 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 bytes
Fp.readline ([size]) #读一行, if size is defined, it is possible to return only one part of the line
Fp.readlines ([size ]) #把文件每一行作为一个list的一个成员, and returns the list in fact its contents are implemented by looping ReadLine (), and if the size parameter is supplied, size indicates the total length of the read content, that is, it may be read only to a portion of the file
Fp.write (str) #把str写到文件中, write () does not add a newline character after str
Fp.writelines (seq) #把seq内容全部写道文件中 (multiple lines of one-time write), without adding anything
Fp.close () #关闭文件. Python automatically shuts down after a file is not used, but this feature does not guarantee that
Fp.flush () #把缓冲区的内容写入磁盘
Fp.fileno () #返回一个长整形的文件标签
Fp.isatty () # Whether the file is an end device file
Fp.tell () #返回文件操作标记的当前位置, #返回下一行 the beginning of the file as the Origin
Fp.next (), and shifts the file action marker to the next line
Fp.seek (offset[,whence ]) #将文件操作标记移到offset的位置, this offset is generally calculated relative to the beginning of the file, generally a positive number, but if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 means that the current position is calculated as the origin. 2 Indicates the origin is calculated at the end of the file. Note that if the file is opened in a or a + mode, the file action tag will automatically return to the end of the file
Fp.truncate ([size]) #把文件裁成规定的大小, and the default is to crop to the position of the current action marker
Directory Operations:
Os.mkdir ("file") #创建目录
Shutil.copyfile ("Oldfile", "NewFile") #oldfile和newfile只能是文件
Shutil.copy ("Oldfile", "NewFile") #oldfile只能是文件夹, NewFile can be a file or destination directory
Shutil.copytree ("Olddir", "Newdir") #复制文件夹, newdir must not exist
Os.rename ("Oldname", "NewName") #重命名文件或文件夹
Shutil.move ("Oldpos", "Newpos") #移动文件或目录
Os.remove ("file") #删除文件
Os.rmdir ("dir") #只能删除空目录
Shutil.rmtree ("dir") #有内容或空目录都可以删
Os.chdir ("path") #换路径
Python file processing