Python folder traversal, file manipulation, get file modification creation time

Source: Internet
Author: User

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
    1. #! /usr/bin/env python
    2. # Coding:utf-8
    3. Import Os,datetime
    4. Base_dir = ' c:/'
    5. List = Os.listdir (Base_dir)
    6. FileList = []
    7. For I in range (0, Len (list)):
    8. Path = Os.path.join (Base_dir,list[i])
    9. if Os.path.isfile (path):
    10. Filelist.append (List[i])
    11. For I in range (0, Len (filelist)):
    12. Path = Os.path.join (Base_dir, Filelist[i])
    13. if Os.path.isdir (path):
    14. Continue
    15. timestamp = os.path.getmtime (path)
    16. Print Timestamp
    17. Ts1 = Os.stat (path). st_mtime
    18. Print Ts1
    19. Date = Datetime.datetime.fromtimestamp (timestamp)
    20. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.