问题;查找目录结构中特定日期之后更改的文件/文件夹,并将这些文件/文件夹复制到另一个位置(如标题中所述)(:
通过以下内容;
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
我可以确定在特定日期更改了哪些文件并将其复制到某个位置。我想要实现的是保持目录结构。一个例子如下;
/testfolder
..somefile1
..somefile2
../testfolder2
....somefile3
....somefile4
依旧......
假设somefile3在指定日期更改,我会将其保存到另一个位置,但在保存时,也应保持级联目录结构。我怎样才能以优雅的方式实现这一目标?
答案 0 :(得分:1)
在复制之前,你应该解决阅读问题。 os.listdir
只会给您一个级别,而os.walk
会让您浏览每个文件中的每个文件。
要复制,您将首先使用os.makedirs(target-path)
在任意深度创建从根目录到目标文件夹的所有文件夹,然后您将使用shutil.copy
复制文件。