Python Forum
Placing directory in a function
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Placing directory in a function
#1
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)
Reply
#2
(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')
Reply
#3
Thanks, I tried using os.chdir("..") and its working !!
Reply
#4
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What default directory does "open" function draw from? Athenaeum 4 3,760 Oct-07-2017, 06:15 AM
Last Post: Skaperen
  placing module level statements into def michaelcollier 7 5,847 May-01-2017, 03:42 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020