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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/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.
1
2
3
4
5
6
7
>>> 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:
1
2
3
4
5
......
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
  Placing image button on top of background image Sintek 1 570 Mar-02-2025, 05:36 PM
Last Post: Sintek
  What default directory does "open" function draw from? Athenaeum 4 5,017 Oct-07-2017, 06:15 AM
Last Post: Skaperen
  placing module level statements into def michaelcollier 7 7,480 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