如何在原始模式下访问 sys.argv (或任何字符串变量)?
我在解析作为参数发送的文件路径时遇到困难:
如果我输入:
os.path.normpath('D:\Data2\090925')
显然
'D:\\Data2\x0090925'
,文件夹名称中的 \0 会扰乱格式。我可以用以下方法纠正它:
os.path.normpath(r'D:\Data2\090925')
这给出了
'D:\\Data2\\090925'
我的问题是,如何使用 sys.argv 获得相同的结果?即:
os.path.normpath(sys.argv[1])
我找不到一种方法将 sys.argv 以原始模式输入 os.path.normpath() 以避免以零开头的文件夹出现问题!
另外,我知道我可以使用 python script.py D:/Data2/090925 来提供脚本,它会完美地工作,但不幸的是 Windows 系统顽固地为我提供 '\' ,而不是“/”,所以我真的需要解决这个问题而不是避免它。
更新1补充: 如果我使用脚本 test.py:
import os, sys
if __name__ == '__main__':
print 'arg 1: ',sys.argv[1]
print 'arg 1 (normpath): ',os.path.normpath(sys.argv[1])
print 'os.path.dirname :', os.path.dirname(os.path.normpath(sys.argv[1]))
我得到以下信息:
C:\Python>python test.py D:\Data2\091002\
arg 1: D:\Data2\091002\
arg 1 (normpath): D:\Data2\091002
os.path.dirname : D:\Data2
即:我丢失了 091002...
UPDATE2: 正如下面的评论告诉我的那样,我给出的示例的问题已解决normpath 被删除:
import os, sys
if __name__ == '__main__':
print 'arg 1: ',sys.argv[1]
print 'os.path.dirname :', os.path.dirname(sys.argv[1])
print 'os.path.split(sys.argv[1])):', os.path.split(sys.argv[1])
这给出:
C:\Python>python test.py D:\Data2\091002\
arg 1: D:\Data2\091002\
os.path.dirname : D:\Data2\091002
os.path.split : ('D:\\Data2\\090925', '')
如果我使用 D:\Data2\091002 :
C:\Python>python test.py D:\Data2\091002
arg 1: D:\Data2\091002
os.path.dirname : D:\Data2
os.path.split : ('D:\\Data2', '090925')
这是我可以使用的东西:谢谢!
I'm having difficulties parsing filepaths sent as arguments:
If I type:
os.path.normpath('D:\Data2\090925')
I get
'D:\\Data2\x0090925'
Obviously the \0 in the folder name is upsetting the formatting. I can correct it with the following:
os.path.normpath(r'D:\Data2\090925')
which gives
'D:\\Data2\\090925'
My problem is, how do I achieve the same result with sys.argv? Namely:
os.path.normpath(sys.argv[1])
I can't find a way for feeding sys.argv in a raw mode into os.path.normpath() to avoid issues with folders starting with zero!
Also, I'm aware that I could feed the script with python script.py D:/Data2/090925
, and it would work perfectly, but unfortunately the windows system stubbornly supplies me with the '\', not the '/', so I really need to solve this issue instead of avoiding it.
UPDATE1 to complement:
if I use the script test.py:
import os, sys
if __name__ == '__main__':
print 'arg 1: ',sys.argv[1]
print 'arg 1 (normpath): ',os.path.normpath(sys.argv[1])
print 'os.path.dirname :', os.path.dirname(os.path.normpath(sys.argv[1]))
I get the following:
C:\Python>python test.py D:\Data2\091002\
arg 1: D:\Data2\091002\
arg 1 (normpath): D:\Data2\091002
os.path.dirname : D:\Data2
i.e.: I've lost 091002...
UPDATE2: as the comments below informed me, the problem is solved for the example I gave when normpath is removed:
import os, sys
if __name__ == '__main__':
print 'arg 1: ',sys.argv[1]
print 'os.path.dirname :', os.path.dirname(sys.argv[1])
print 'os.path.split(sys.argv[1])):', os.path.split(sys.argv[1])
Which gives:
C:\Python>python test.py D:\Data2\091002\
arg 1: D:\Data2\091002\
os.path.dirname : D:\Data2\091002
os.path.split : ('D:\\Data2\\090925', '')
And if I use D:\Data2\091002 :
C:\Python>python test.py D:\Data2\091002
arg 1: D:\Data2\091002
os.path.dirname : D:\Data2
os.path.split : ('D:\\Data2', '090925')
Which is something I can work with: Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“丢失”路径的最后一部分与 sys.argv 中的转义(或缺少转义)无关。
如果您使用
os.path.normpath()
,然后使用os.path.dirname()
,这是预期的行为。"Losing" the last part of your path is nothing to do with escaping (or lack of it) in
sys.argv
.It is the expected behaviour if you use
os.path.normpath()
and thenos.path.dirname()
.以下是在目录路径末尾添加反斜杠的 Python 代码片段:
Here is a snippet of Python code to add a backslash to the end of the directory path: