Recently the use of os.remove
an error when deleting a directory, a common method os operating under the file or directory record here
- Recursively delete a file or folder
import shutil
shutil.rmtree()
Rename the file
os.rename('a.txt','b.txt')
- Delete files
os.remove('./abc/a.txt') 只能删除文件
- Delete directory will complain:
PermissionError: [WinError 5] 拒绝访问。: '111'
Create a directory
os.mkdir('xxx')
os.mkdir('aaa/bbb')
#aaa must be present in order to create successRecursively create a directory
os.makedirs('111/222/333')
os.makedirs('111/222/333',exist_ok=True)
# presence will not be createdDelete the directory
os.rmdir('aaa/bbb')
# delete aaa bbb directory under the directory
can only delete empty directories, otherwise it will errorDelete levels of directories
if the directory is empty, delete, and recursively to the parent directory, should also empty, delete, and so on.
If the parent directory is not empty then stop deleting files. If the 333 error is not empty.
os.removedirs('111/222/333')
Get the current directory
os.getcwd()
Get a directory listing
os.listdir()
Change directory
os.chdir()
Whether a file or folder exists
os.path.exists('111/222')
To determine whether the file
os.path.isfile()
Determine whether the folder
os.path.isdir()
Gets the absolute path
os.path.abspath()
Determine whether the absolute path
os.path.isabs()
Gets the last part of the path
os.path.basename()
Get the path portion of the path
os.path.dirname()
- Getting file information
- getatime return file or directory path points to the last access time
import os import time # 最后访问时间 ret = os.path.getatime('file') ret1 = time.strftime('%Y-%m-%d %X',time.gmtime(ret)) print(ret1) # 2019-01-10 12:26:55
- getctime () to view the file creation time, return time stamp
ret = os.path.getctime('file') print(ret)
- getsize () to view the file size
ret = os.path.getsize('file/node.txt') print(ret)
- getmtime () returns the file or directory path points to the last modification time
ret = os.path.getmtime('file') print(ret) # 1547123215.5290873