简体   繁体   中英

Shutil.move() function moved files into a folder which i cant open

I used shutil.move() function to move some important files into desirable directory. The directory did not exist, So i gave the name as a parameter i shutil.move() function. After the execution, it moved the files in a dir(Which does not seem to be a dir, when seeing properties it says its a "File") which i cannot open. can you please help me to open this file to extract my files?? The code was like this:

for file in Path("C/Users/Mahum/Downloads").glob(*.exe):
    shutil.move(file, "E/New Folder")

The code executed with zero exit code but i got that problem.....

I think you have misunderstood what shutil can do.

  • If the destination is a dir, then it will move the source file into
    that dir.

  • However if the destination doesnt exist, it will rename the src as
    the destination.

  • If the destination does exist and its a file it will overwrite that dest file with the source file.

So in you case lets say you had 5 files. "E/New Folder" didnt exist. So on the file loop

file1 is renamed as E/New Folder (at this point file1 doesnt exist and its contents are in E/New Folder)

file2 overwrites E/New Folder (file2 is removed and E/New Folder now has the contents of file2 and file1 contents are lost completely now)

file3 overwrites E/New Folder (file3 is removed and E/New Folder now has the contents of file3 and file2 contents are lost completely now)

file4 overwrites E/New Folder (file4 is removed and E/New Folder now has the contents of file4 and file3 contents are lost completely now)

file5 overwrites E/New Folder (file5 is removed and E/New Folder now has the contents of file5 and file4 contents are lost completely now)

So chances are E/New Folder is indeed a file and will hold the contents of the list file that went through shutil.move to this destination and all other files and contents are lost.

你的路径应该是C:/Users/Mahum/Downloads而不是C/Users/Mahum/Downloads

If you didn't create the new folder before you moved the files it may have treated the request as a file and overwritten ontop of each other. Another thing that may have ocurred it just overwitten ontop of each file. If you didn't have a back up you may have lost the files. You can try to recover with a disk recovery program. Look for one for OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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