简体   繁体   English

使用python的shutil.move移动文件

[英]move files with python's shutil.move

I'm new in Python, and I want to use Python make my work easier.But I got a big trouble!!! 我是Python新手,我想使用Python使我的工作更轻松,但是我遇到了很大的麻烦! I have a lot of files in different floders, and I want move these files into a new floder, But I forgot mkdir, this means the dest_path is not exit...After the code running , I can't find my files anymore!!!!! 我在不同的目录中有很多文件,我想将这些文件移到新的目录中,但是我忘记了mkdir,这意味着dest_path不会退出...在代码运行之后,我再也找不到文件了! !!!! these Files are truly important for me .Please help me to find these files back. 这些文件对我来说确实很重要。请帮助我找回这些文件。 Thanks a lot.my code as below: 非常感谢我的代码如下:

path = r'H:\oldpath'
dest_hj = r'H:newfloder'
os.mkdir(dest_hj)# I forgot add this line !!!, the dest_path not exist
files = os.listdir(path)
for file in files:
    p = path+'\\'+file
    if os.path.isdir(p) == True:
        pfiles = os.listdir(p)
        for f in pfiles:
            if f.__contains__("abc"):
                print(p+'\\'+f+':processing')
                shutil.move(p+'\\'+f,dest_hj)

after move , I got a file, but cant open it, also don't know what file type it is 移动后,我得到了一个文件,但无法打开它,也不知道它是什么文件类型

The files have most likely been copied to a file named as your desired destination folder. 这些文件很可能已复制到名为所需目标文件夹的文件中。

Unlucky every new source file overwrote the file copied before. 不幸的是,每个新的源文件都会覆盖之前复制的文件。 So with some luck you might be able to recover the last file from there. 因此,如果运气好的话,您也许可以从那里恢复最后一个文件。

To recover the other files you should check the options for file recovery on you operating system. 要恢复其他文件,应在操作系统上检查文件恢复选项。 But this is out of the scope of SO. 但这超出了SO的范围。

At the end a general advice for software development: you do not test your code on production data and you always have a backup and a recovery strategy for you production data. 最后,提供了有关软件开发的一般建议:您无需在生产数据上测试代码,并且始终拥有生产数据的备份和恢复策略。

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

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