Below is a small test routine which takes a path to a file and moves the file up one directory. I am using the os and shutil modules, is there one module that could perform this task? Is there a more pythonic way of implementing this functionality?
The code below is running on windows, but the best cross-platform solution would be appreciated.
def up_one_directory(path):
"""Move file in path up one directory"""
head, tail = os.path.split(path)
try:
shutil.move(path, os.path.join(os.path.split(head)[0], tail))
except Exception as ex:
# report
pass
This is the same as @thierry-lathuille's answer, but without requiring shutil
:
p = Path(path).absolute()
parent_dir = p.parents[1]
p.rename(parent_dir / p.name)