Reputation: 588
I have a path /foo/bar/poo/car
and there are files in the directory car
.
I want to end up with /foo/bar
with the same files in bar
. I realized this is wrong since it doesn't maintain the files:
>>> import os
>>> os.path.dirname(os.path.dirname('/foo/bar/poo/car'))
'/foo/bar'
I guess I would have to first move the files from car to bar and then do the above? Is there a cleaner or easier way to perform this?
Upvotes: 0
Views: 519
Reputation: 1410
You must get all the files absolute paths first with glob. Then you can move these files with shutil.move
import glob
import os
import shutil
source_dir = r'R:/foo/bar/poo/car'
dest_dir = r'R:/foo/bar'
# get all file path
all_files_path = glob.glob(os.path.join(source_dir, '*.*'))
# move the files to the new dir
for file_path in all_files_path:
shutil.move(file_path, dest_dir)
Upvotes: 1
Reputation: 62
Use pathlib.Path
:
from pathlib import Path
p = Path('/foo/bar/poo/car')
bar = p.parent.parent
print(bar)
Output:
/foo/bar
To move files, use shutil.move
.
Upvotes: 1