views:

291

answers:

3

I have lots of directories with text files written using (g)vim, and I have written a handful of utilities that I find useful in Python. I start off the utilities with a pound-bang-/usr/bin/env python line in order to use the Python that is installed under cygwin. I would like to type commands like this:

%cd ~/SomeBook

%which pythonUtil

/usr/local/bin/pythonUtil

%pythonUtil ./infile.txt ./outfile.txt

(or % pythonUtil someRelPath/infile.txt somePossiblyDifferentRelPath/outfile.txt)

pythonUtil: Found infile.txt; Writing outfile.txt; Done (or some such, if anything)

However, my pythonUtil programs keep telling me that they can't find infile.txt. If I copy the utility into the current working directory, all is well, but then I have copies of my utilities littering the landscape. What should I be doing?

Yet Another Edit: To summarize --- what I wanted was os.path.abspath('filename'). That returns the absolute pathname as a string, and then all ambiguity has been removed.

BUT: IF the Python being used is the one installed under cygwin, THEN the absolute pathname will be a CYGWIN-relative pathname, like /home/someUser/someDir/someFile.txt. HOWEVER, IF the Python has been installed under Windows (and is here being called from a cygwin terminal commandline), THEN the absolute pathname will be the complete Windows path, from 'drive' on down, like D:\cygwin\home\someUser\someDir\someFile.txt.

Moral: Don't expect the cygwin Python to generate a Windows-complete absolute pathname for a file not rooted at /; it's beyond its event horizon. However, you can reach out to any file on a WinXP system with the cygwin-python if you specify the file's path using the "/cygdrive/driveLetter" leadin convention.

Remark: Don't use '\'s for separators in the WinXP path on the cygwin commandline; use '/'s and trust the snake. No idea why, but some separators may be dropped and the path may be modified to include extra levels, such as "Documents and Settings\someUser" and other Windows nonsense.

Thanks to the responders for shoving me in the right direction.

+3  A: 

Look at os.getcwd:

Edit: For relative paths, please take a look at the os.path module:

in particular, os.path.join and os.path.normpath. For instance:

import os
print os.path.normpath(os.path.join(os.getcwd(), '../AnotherBook/Chap2.txt'))
ars
Great minds ... Would I be right to assume there's something in there that handles relative paths, too? Guess I'll go look.
behindthefall
:) Just updated the answer for relative paths.
ars
Thanks! os.path.abspath('path') appeals to me. Kind of takes away all that uncertainty, and I didn't have to type a thing.
behindthefall
Heh, heh: take that, you big, ugly, one-drive-and-seven-directories-deep-cygwin-inside-XP absolute path, you.
behindthefall
Guess you get the "answer-prize"!
behindthefall
A: 

What happens when you type "ls"? Do you see "infile.txt" listed there?

Michael Aaron Safyan
Yes. The text files in fact are in the, e.g., ~/SomeBook directory. (I would be quite upset if they were not!)
behindthefall
A: 
os.chdir(my_dir)

or

os.chdir(os.getcwd())
Hameltime
Does that address the relative path situation?
behindthefall