简体   繁体   中英

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!!! 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!!!!! 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.

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.

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