Question

I have a program that I wrote that goes through all of the files in a directory and looks for files with a flag and then inputs them into a different program. It works great, the only thing that I am trying to do now it put the script in one location on a box, and then have it know to look in the directory that I am currently in as the working directory. Currently all I do is mv the script into whatever directory I am working in and just call it from there, but that is tedious and requires me to constantly be cp'ing the script.

I am just hoping there is a little more elegant way to do this? Any help would be appreciated.

Was it helpful?

Solution

what about

import os

loc = os.getcwd()

OTHER TIPS

The __file__ object can return such information:

import os

os.path.dirname(__file__)

If you are comfortable passing the working directory as an argument to the script, the following approach will work.

#!/usr/bin/env python
import sys
import os

workingDir = sys.argv[1]
os.chdir (workingDir)

# Your code here

Sounds like a perfect use for os.walk() the example from the help would be a good starting point:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top