python - how to access sys.argv (or any string variable) in raw mode? -


I'm having trouble parsing the file path sent as a plea:

if I type:

  os.path.normpath ('D: \ Data2 \ 090925')  

I'm getting

< Pre> 'd: \\ data2 \ x0090 925'

Obviously disturbing formatting \ 0 in folder name, I can fix it with the following :

  os.path.normpath (r'D: \ Data2 \ 090925 ')  

which gives

  'D: \\ Data 2 \\ 090 925' ' 

My problem Or, how do I get the same result with sys.argv? Meaning:

  os.path.normpath (sys.argv [1])  

I can not find a way to feed sys.argv In os.path.normpath () in raw mode to avoid problems with folders starting from zero!

In addition, I know that I can feed the script with python script.py D: / data 2/090 925 , and it will work perfectly , But unfortunately the windows system stubbornly supplies me with '\', not '/', so I really have to solve this issue instead of avoiding it.

UPDATE1 supplement: If I use scripts 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: \ data 2 \ 091002 \ arg 1: d: \ data 2 \ 091002 \ argatus 1 (honorary): d: \ data 2 \ 091002 os.path.dirname: d: \ data2  

i.e.: I've lost 091002 ...

UPDATE2 : As noted below I have been notified, this problem is solved for me, for example when the standard path 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  
  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', '090 925')  

Who can I work with: Thanks!

"lose the last part" of your path from sys.argv There is nothing to avoid (or lack thereof) to escape.

If you use os.path.normpath, then this is expected behavior () and then os.path.dirname () .

  & gt; & Gt; & Gt; Import OS & gt; & Gt; & Gt; Os.path.normpath ("c: / foo / bar /") 'c: \\ foo \\ bar' & gt; & Gt; & Gt; Os.path.dirname ('c: \\ foo \\ bar') 'c: \\ foo'  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -