如何告诉 Python 脚本 (cygwin) 在当前(或相对)目录中工作?
我有很多目录,其中包含使用 (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 someRelPath/infile.txt somePossibilityDifferentRelPath/outfile.txt) txt)
pythonUtil: 在file.txt中找到; 写入outfile.txt; 完成(或类似的,如果有的话)
但是,我的 pythonUtil 程序不断告诉我他们找不到 infile.txt。 如果我将实用程序复制到当前工作目录中,一切都很好,但随后我的实用程序副本散落在各处。 我应该做什么?
另一个编辑:总结一下——我想要的是 os.path.abspath('filename')。 这将以字符串形式返回绝对路径名,然后所有歧义都被删除。
但是:如果正在使用的 Python 是安装在 cygwin 下的 Python,那么绝对路径名将是 CYGWIN 相对路径名,例如 /home/someUser/someDir/someFile.txt。 但是,如果 Python 已安装在 Windows 下(并且在此处从 cygwin 终端命令行调用),则绝对路径名将是完整的 Windows 路径,从“drive”向下,如 D:\cygwin\home\someUser \someDir\someFile.txt。
寓意:不要期望 cygwin Python 为不以 / 为根的文件生成 Windows 完整的绝对路径名; 它超出了它的事件视界。 但是,如果使用“/cygdrive/driveLetter”引导约定指定文件的路径,则可以使用 cygwin-python 访问 WinXP 系统上的任何文件。
备注:在 cygwin 命令行中的 WinXP 路径中不要使用“\”作为分隔符; 使用“/”并信任蛇。 不知道为什么,但一些分隔符可能会被删除,并且路径可能会被修改以包含额外的级别,例如“Documents and Settings\someUser”和其他 Windows 废话。
感谢回复者将我推向正确的方向。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 os.getcwd:
编辑:对于相对路径,请查看 os.path 模块:
特别是 os.path.join 和 os.path.normpath。 例如:
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:
当你输入“ls”时会发生什么? 您看到那里列出的“infile.txt”了吗?
What happens when you type "ls"? Do you see "infile.txt" listed there?
或者
or