python - Python3: Files organizing -
i got path of file on unix system:
folder1/folder2/text.txt
folder1 contains folder2, , folder2 contains file. path file test.txt can shorter -> folder1/text.txt, need universal solution:
if(folder contains 1 subfolder)-> move files subfolder folder , del subfolder
from comment see want move files in folder3 folder2. here program move files in folders below folder1, delete folders (but not folder1.) first have change directory folder1. , if of files want move exist in folder1 already, error message. can fix though. put in print statements can see working.
import os, shutil p = os.path._getfullpathname("folder1") print p root, dirs, files in os.walk(p, topdown=false): file in files: k = os.path.join(root,file) print k shutil.move(k, p) dir in dirs: k = os.path.join(root,dir) print k os.rmdir(k)
Comments
Post a Comment