繁体   English   中英

如何使用 Python 2.7 创建一个将文件夹中的最新文件复制到另一个文件夹的函数?

[英]How can I create a function that copies the newest file in a folder to another folder using Python 2.7?

再会

我正在尝试创建一个使用 Python 2.7 将文件夹中的最新文件复制到另一个文件夹的程序。 我已经能够使用以下代码复制指定的文件:

def copy():
   # copies actual file
   shutil.copy(input, output)


print(copy())

但是,我需要确定文件夹中的最新文件(创建日期)并将该文件复制到新文件夹中。

我已经能够使用以下代码提取特定文件的文件名和创建日期:

def copy():
   # variables for copying
   input = r"C:\Users\micha\Desktop\Test_Copy1\Test1.txt"
   output = r"C:\Users\micha\Desktop\Test_Copy2"
   path = r"C:\Users\micha\Desktop\Test_Copy1"
   h ="C:\Users\micha\Desktop\PythonTools\Test1.txt"

   # copies actual file
   # shutil.copy(input, output)

   timestamp = os.path.getctime(input)
   convert = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
   name = os.path.basename(input)

   print name
   print convert


print(copy())

但是,我不确定如何对文件夹中的所有文件执行此分析,然后确定要复制到新文件夹的正确文件。

如果有人可以提供帮助,将不胜感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM