如何告诉Python脚本(cygwin)在当前(或相对)目录中工作?

时间:2022-03-11 07:11:47

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:

我有很多目录,其中包含使用(g)vim编写的文本文件,我编写了一些我认为在Python中有用的实用程序。我使用pound-bang- / usr / bin / env python行启动实用程序,以便使用cygwin下安装的Python。我想输入这样的命令:

%cd ~/SomeBook

%which pythonUtil

/usr/local/bin/pythonUtil

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

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

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

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

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

pythonUtil:找到infile.txt;写outfile.txt;完成(或某些此类,如果有的话)

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?

但是,我的pythonUtil程序一直告诉我他们找不到infile.txt。如果我将该实用程序复制到当前的工作目录中,一切都很好,但是我有我的实用程序的副本乱扔垃圾。我该怎么办?

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.

然而另一个编辑:总结一下 - 我想要的是os.path.abspath('filename')。将绝对路径名作为字符串返回,然后删除所有歧义。

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.

但是:如果使用的Python是在cygwin下安装的那个,那么绝对路径名将是一个CYGWIN相对路径名,如/home/someUser/someDir/someFile.txt。但是,如果Python已经安装在Windows下(并且在这里从cygwin终端命令行调用),那么绝对路径名将是完整的Windows路径,从“驱动器”开启,如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.

道德:不要指望cygwin Python为不以/为根的文件生成Windows完整的绝对路径名;它超越了它的事件视野。但是,如果使用“/ cygdrive / driveLetter”leadin约定指定文件的路径,则可以使用cygwin-python访问WinXP系统上的任何文件。

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.

备注:不要在cygwin命令行的WinXP路径中使用'\'作为分隔符;使用'/'并信任蛇。不知道为什么,但可能会删除一些分隔符,并且可能会修改路径以包含额外的级别,例如“Documents and Settings \ someUser”和其他Windows废话。

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

感谢响应者将我推向了正确的方向。

3 个解决方案

#1


Look at os.getcwd:

看看os.getcwd:

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

编辑:对于相对路径,请查看os.path模块:

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

特别是os.path.join和os.path.normpath。例如:

import os
print os.path.normpath(os.path.join(os.getcwd(), '../AnotherBook/Chap2.txt'))

#2


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

键入“ls”会发生什么?你看到那里列出的“infile.txt”了吗?

#3


os.chdir(my_dir)

or

os.chdir(os.getcwd())

#1


Look at os.getcwd:

看看os.getcwd:

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

编辑:对于相对路径,请查看os.path模块:

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

特别是os.path.join和os.path.normpath。例如:

import os
print os.path.normpath(os.path.join(os.getcwd(), '../AnotherBook/Chap2.txt'))

#2


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

键入“ls”会发生什么?你看到那里列出的“infile.txt”了吗?

#3


os.chdir(my_dir)

or

os.chdir(os.getcwd())