Home > Software engineering >  Files moved to unknown location with shutil.move()
Files moved to unknown location with shutil.move()

Time:07-22

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.

CodePudding user response:

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

CodePudding user response:

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 this doesn't help add:

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

This above will lead to solving the problem described in the question, but in a general case, if even this doesn't help, switch to a shell input prompt and execute:

On Linux:

$ sudo updatedb 
$ locate -b '\fileName.ext'

On MS Windows from the root of C:/ or D:/ or ... run:

$ dir fileName.ext /s

And yes,

The simple is sometimes the answer but we don't see it

because the question provides enough information to find out that the file will be moved to the as current working directory (in this case the folder of python file) considered folder.

Anyway, the steps mentioned, especially the last one, are a good way to find out where a file or directory was gone.

  • Related