Python Forum
Placing directory in a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Placing directory in a function (/thread-11336.html)



Placing directory in a function - mmaz67 - Jul-04-2018

Hi i am currently trying to put 2 directories that i am going to use in 2 different functions.

These are the location:
/disks/user/mmaz/mmaz05/rundir
/disks/user/mmaz/mmaz05/in_file

The script is currently in /disks/user/mmaz/mmaz05/rundir. I am having problem with the second function on how do i go one level up to /in_file directory?

#!/usr/bin/python

import os
import os.path

myPWD = os.getcwd()

def run_directory( str ):
	print str
	return
	
def infile_directory (str ):
	#need to go one step up to infile directory
	print str
	return

run_dir = run_directory(myPWD)
infile_dir = infile_directory(myPWD)



RE: Placing directory in a function - snippsat - Jul-04-2018

(Jul-04-2018, 03:39 AM)mmaz67 Wrote: how do i go one level up to /in_file directory?
You can use os.chdir("..")
Can also use pathlib which has a parent method.
>>> from pathlib import Path, PurePath
>>> 
>>> p = PurePath(Path.cwd())
>>> p
PurePosixPath('/home/mint')
>>> p.parent
PurePosixPath('/home')



RE: Placing directory in a function - mmaz67 - Jul-04-2018

Thanks, I tried using os.chdir("..") and its working !!


RE: Placing directory in a function - volcano63 - Jul-04-2018

(Jul-04-2018, 03:39 AM)mmaz67 Wrote:
......
def run_directory( str ):
	print str
	return
......	

str is python built-in function - it is not recommended to use standard names as variable/argument names