简体   繁体   English

使用 shutil.move() 将文件移动到未知位置

[英]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:".我与shutil移动,但命运路径没有提到“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.此代码按其 gmtime 将文件移动到dst文件夹,以按创建月份日期组织。 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. shutil需要完整的路径才能移动,但我不明白为什么会这样,文件只是移动到未知路径,只有“创建月份日期”的路径。 To where were my files moved?我的文件被移到了哪里?

I can share the complete python code if you need it.如果您需要,我可以分享完整的 python 代码。

Console shows:控制台显示:

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

The "2022 06" is the path printed for dst variable. “2022 06”是为dst变量打印的路径。

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. python代码将文件发送到python文件的文件夹。 Next time we try, considere that without complete path shutil.move() acts over the file path.下次我们尝试时,请考虑没有完整路径 shutil.move() 作用于文件路径。

Screenshot of where my files was我的文件所在位置的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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