shutil.move deleted all my photos

2019-06-09 17:48发布

问题:

for i in os.listdir():
   if "jpeg" in i or "png" in i:
      shutil.move(os.path.join(os.getcwd(),i),"my photos")

I wanted to move all photos to a folder called "my photos" which is not exist. It created a txt file called "my photos" but it is just 300 kb and when I quickview it it only shows one of my photo. So how can I get my photos back? where are they now?

回答1:

I believe shutil.move simply took each file and interpreted "my photos" as the name to save each file as rather than a directory to copy it into. For each image in the directory, it would have overwritten it, leaving "my photos" with just the contents of the last image copied.

Looking at examples like this of how people have used shutil for moving files, they join the base directory name and the file name to form the target parameter.

I hope you had backups.