Delete files created before a point in time in the local directory with Python

Source: Internet
Author: User

  • Reference http://www.cnblogs.com/iderek/p/8035757.html
  • 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) #获取文件的创建时间

Method One

Import Shutil, OS

Os.chdir (' D:\_data\python\os ') #进入要清空的目录
ds = List (Os.listdir ()) #获得该目录下所有文件或文件夹列表
For D in DS: #遍历该列表
If Os.path.isfile (d): #如果列表项是文件
Os.remove (d) #直接删除
else: #如果不是文件, it must be a folder
Shutil.rmtree (d) #也直接删除
Method Two
Import OS, datetime

dirtobeemptied = ' D:\_data\python\os ' #需要清空的文件夹

ds = List (Os.walk (dirtobeemptied)) #获得所有文件夹的信息列表
Delta = Datetime.timedelta (days=365) #设定365天前的文件为过期
now = Datetime.datetime.now () #获取当前时间

For D in DS: #遍历该列表
Os.chdir (D[0]) #进入本级路径 to prevent file errors from being found
If d[2]! = []: #如果该路径下有文件
For x in d[2]: #遍历这些文件
CTime = Datetime.datetime.fromtimestamp (Os.path.getctime (x)) #获取文件创建时间
If CTime < (Now-delta): #若创建于delta天前
Os.remove (x) #则删掉

Delete files created before a point in time in the local directory with Python

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.