简体   繁体   中英

Files moved to unknown location with shutil.move()

I have a lack of knowledge - I think this had to gave me an error, but it didn't.

I move with shutil but the destiny path is without the reference to "C:". See the code:

src = os.path.join(path, os.path.basename(file))
dst = os.path.join(time.strftime('%Y %m', time.gmtime(os.path.getmtime(path+'\\'+file))))
if os.path.exists(dst):
    shutil.move(src, dst)

This code move the files to the dst folder by its gmtime, to organize by creation month date. I am trying to organize my photos and registers. shutil needs the complete path to move, but I don't understand why this happened, the files just moved to unkown path, a path with the "creation month date" only. To where were my files moved?

I can share the complete python code if you need it.

Console shows:

C:\folder1\folder2\sourcepath\filename.txt
 2022 06

The "2022 06" is the path printed for dst variable.

To where were my files moved?

Find out by yourself adding a print command ( print debugging ):

src = os.path.join(path, os.path.basename(file))
dst = os.path.join(time.strftime('%Y %m', time.gmtime(os.path.getmtime(path+'\\'+file))))
if os.path.exists(dst):
    print(src, dst)
    shutil.move(src, dst)

(sometimes the most simple things doesn't come to mind ...)

And if the above alone doesn't help add:

print(os.getcwd()) # prints the current working directory

Solved problem! The simple is sometimes the answer but we don't see it because thinked complex. The python code sends the files to the folder of python file. Next time we try, considere that without complete path shutil.move() acts over the file path.

Screenshot of where my files was

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