In Python, the file operations are mainly from the OS module, the main methods are as follows:
Os.listdir (dirname): List directories and files under DirName
OS.GETCWD (): Get current working directory
Os.curdir: Returns the current directory ('. ')
Os.chdir (dirname): Changing the working directory to DirName
Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false
Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false
Os.path.exists (name): Determine if there is a file or directory name
Os.path.getsize (name): Get file size, if name is directory return 0L
Os.path.abspath (name): Get absolute path
Os.path.normpath (PATH): Canonical path string form
Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
Os.path.splitext (): Detach file name and extension
Os.path.join (path,name): Connection directory with file name or directory
Os.path.basename (PATH): Return file name
Os.path.dirname (PATH): Return file path
Os.remove (dir) #dir为要删除的文件夹或者文件路径
Os.rmdir (Path) #path要删除的目录的路径. It should be explained that the directory deleted using Os.rmdir must be an empty directory, or the function will fail.
Os.path.getmtime (name) # Gets the file modification time
Os.stat (path). st_mtime# Gets the modified time of the file
Os.stat (path). St_ctime #获取文件修改时间
Os.path.getctime (name) #获取文件的创建时间
The code to list the modified time in the folder is as follows:
[Python]View PlainCopy
- #! /usr/bin/env python
- # Coding:utf-8
- Import Os,datetime
- Base_dir = ' c:/'
- List = Os.listdir (Base_dir)
- FileList = []
- For I in range (0, Len (list)):
- Path = Os.path.join (Base_dir,list[i])
- if Os.path.isfile (path):
- Filelist.append (List[i])
- For I in range (0, Len (filelist)):
- Path = Os.path.join (Base_dir, Filelist[i])
- if Os.path.isdir (path):
- Continue
- timestamp = os.path.getmtime (path)
- Print Timestamp
- Ts1 = Os.stat (path). st_mtime
- Print Ts1
- Date = Datetime.datetime.fromtimestamp (timestamp)
- print list[i],' Last modified time is: ', Date.strftime ('%y-%m-%d%h:%m:%s ')
Python folder traversal, file manipulation, get file modification creation time