python os template manipulating files and directories common methods
Others
2019-07-29 04:55:23
views: null
- python common operations files and directories methods:
- Python script to get the current working directory path: os.getcwd ()
- All files and directories return under the specified directory: os.listdir (). For example: Returns the file under the C drive: os.listdir ( "C: \")
- To delete a file: os.remove (filepath)
- To delete multiple empty directory: os.removedirs (r'd: python ')
- Whether given path is a file: os.path.isfile (filepath)
- Whether given path is a directory: os.path.isdir (filepath)
- Determine whether the absolute path: os.isabs ()
- Verify that the path really exists: os.path.exists (). Such as whether there is Python folder Inspection D drive: os.path.exists (r'd: \ python ')
- A separate path of the directory name and file name: os.path.split (). For example: os.path.split (r '/ home / qiye / qiye.txt)
returns a result tuples: (' / home / qiye ' ,' qiye.txt ')
- Extension separation: os.path.splitext (). For example: For example: os.path.split (r '/ home / qiye / qiye.txt)
returns a result tuples: (' / home / qiye / qiye ',' txt '.)
- Get the path name: os.path.dirname (filepath)
- Get File name: os.path.basename (filepath)
- Read and set environment variables: os.getenv () and os.putenv ()
- Rename the file or directory: os.rename (old, new)
- Create multi-level directory: os.makedirs (r'c: \ python \ test ')
- Create a single directory: os.mkdir ( "test")
- Getting file attributes: os.stat (file)
- Get File Size: os.getsize (filename)
- Delete the directory: os.rmdir ( "dir") can only delete empty directories
- Get the file attributes and time stamp: os.chmod (file)
Origin www.cnblogs.com/pengjie-py/p/11261111.html