0

Good Day

I am trying to create a program that copies the newest file in a folder to another folder using Python 2.7. I have been able to copy a specified file using the following code:

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


print(copy())

However I need to identify the newest file(Date Created) within the folder and copy that file to the new folder.

I have been able to extract the filename and the date created of a specific file using the following code:

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())

However I am unsure as to how I would perform this analysis on all the files within a folder and then identifying the right file to copy to the new folder.

If anyone could help it would be much appreciated.

  • 2
    Does this answer your question? [How to get the latest file in a folder using python](https://stackoverflow.com/questions/39327032/how-to-get-the-latest-file-in-a-folder-using-python) – Tomerikoo Feb 21 '20 at 10:15
  • Also, consider moving to a newer version of Python. Python 2 is about to become unsupported, and also - why not?... – Tomerikoo Feb 21 '20 at 10:21

0 Answers0