How to Move to One Folder Back in Python

How to move to one folder back in python

Just like you would in the shell.

os.chdir("../nodes")

Moving up one directory in Python

>>> import os
>>> print os.path.abspath(os.curdir)
C:\Python27
>>> os.chdir("..")
>>> print os.path.abspath(os.curdir)
C:\

Moving a file from one folder to another and back in Python

Alright, so I managed to do this by myself in the end. In case anyone is interested you'll find samples of the code below. It's very rudimentary and can of course be optimized but it does work.

Main:

def Main():

dir_name = input("Enter the destination path: ")
if os.path.isdir(dir_name):
print ("Directory already existed.")
else:
print ("Directory created successfully!")
os.makedirs(dir_name)

choice = input("Would you like to (M)ove or (R)estore?: ")

if choice == 'M':
path = input("Directory of file you want moved: ")
file = input("Name of the file+extension: ")
file_path = path+'/'+file
move(file_path)
print ("Done.")

elif choice == 'R':
with open('quar_id.txt') as f:
quar_id = f.readline()
restore_from_quar(quar_id)
print ("Done.")

else:
print ("No valid option selected, closing...")

Move:

def move(filepath):

f = open('quar_id.txt','w')
f.write(path)
f.close()
os.chdir(dir_name)
shutil.move(file_path,dir_name+'/'+file)

Restore:

def restore(quar_id):

os.chdir(dir_name)
myfile = os.listdir(dir_name)
file = str(myfile)
file = file[2:-2]
shutil.move(file,quar_id+'/'+file)

Python: How to go one folder back in pathlib

Instead of .., do path.parent:

>>> pathlib.Path.cwd()
PosixPath('/home/jan/Downloads')
>>> pathlib.Path.cwd().parent
PosixPath('/home/jan')

For your second attempt, make Python calculate an absolute path from a relative one.

>>> os.path.abspath('../')
'/home/jan'

Also make sure the path you are trying to save to is the actually correct path by printing it.

Using Python's os.path, how do I go up one directory?

os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'templates'))

As far as where the templates folder should go, I don't know since Django 1.4 just came out and I haven't looked at it yet. You should probably ask another question on SE to solve that issue.

You can also use normpath to clean up the path, rather than abspath. However, in this situation, Django expects an absolute path rather than a relative path.

For cross platform compatability, use os.pardir instead of '..'.

Moving all files from one directory to another using Python

Try this:

import shutil
import os

source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'

file_names = os.listdir(source_dir)

for file_name in file_names:
shutil.move(os.path.join(source_dir, file_name), target_dir)

Python: move a file up one directory

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)

Move up directory until folder found python

Inside your files within regression_calcs:

from os import listdir
from os.path import join, isdir, dirname, basename

filepath = None
# get parent of the .py running
par_dir = dirname(__file__)
while True:
# get basenames of all the directories in that parent
dirs = [basename(join(par_dir, d)) for d in listdir(par_dir) if isdir(join(par_dir, d))]
# the parent contains desired directory
if 'Data_Input' in dirs:
filepath = par_dir
break
# back it out another parent otherwise
par_dir = dirname(par_dir)

Of course this only works if you have a single '/Data_Input/' directory!



Related Topics



Leave a reply



Submit