查找在Python中的特定日期之后修改的文件/文件夹 [英] Find files/folders that are modified after a specific date in Python
问题描述
;
def mod():
查找文件修改日期,
latest = 0
now = time.strftime('%Y-%m-%d',time.localtime())
dir =/ home / y33t /
为os.listdir(dir)中的fname:
如果fname.endswith(''):
modtime = os.stat(os.path.join(dir,fname))。st_mtime
如果modtime>最新:
latest = modtime
out = time.strftime('%Y-%m-%d',time.localtime(latest))
if out ==现在:
print fname,今天改变了。
else:
pass
我可以确定哪些文件在特定日期发生了更改,并将其复制到loc中我想达到的目的是保持目录结构。一个例子如下:
$ $ $ $ $ $ c $ / testfolder
..somefile1
..somefile2
../testfolder2
.... somefile3
.... somefile4
等等...
可以说somefile3在指定的日期被更改,我将它保存到另一个位置,但是在保存时,级联的目录结构应该保持为好。我怎样才能实现这个优雅的方式?
在复制之前,你应该解决阅读。 os.listdir
会给你一个级别,而 os.walk
会让你遍历每个文件的深度。复制时,首先使用 os.makedirs(target-path)
从根目录创建所有的文件夹以任何深度的目标文件夹,然后您将使用 shutil.copy
来复制文件。
Problem ; find files/folders that are changed after a specific date from a directory structure and copy these to another location (as stated clearly in title as well) (:
By the following ;
def mod():
"""Find files modified today, given a file path."""
latest = 0
now = time.strftime('%Y-%m-%d', time.localtime())
dir = "/home/y33t/"
for fname in os.listdir(dir):
if fname.endswith(''):
modtime = os.stat(os.path.join(dir, fname)).st_mtime
if modtime > latest:
latest = modtime
out = time.strftime('%Y-%m-%d', time.localtime(latest))
if out == now:
print fname, "has changed today. "
else:
pass
I can determine which files are changed at a specific date and copy these to a location. What I would like to achieve is to keep the directory structure as well. An example is as following ;
/testfolder
..somefile1
..somefile2
../testfolder2
....somefile3
....somefile4
and so on...
lets say somefile3 is changed at the specified date and I will save it to another location but while saving, cascaded directory structure should be maintained as well. How can I achieve this in an elegant way ?
Before copying, you should solve reading. os.listdir
will give you only one level while os.walk
will let you go over each file in every depth.
To copy, you will first use os.makedirs(target-path)
to create all the folders from root to target folder at any depth then you will use shutil.copy
to copy the file.
这篇关于查找在Python中的特定日期之后修改的文件/文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!