AlexandreS
AlexandreS

Reputation: 131

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

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!

Upvotes: 1

Views: 4204

Answers (2)

steveha
steveha

Reputation: 76745

Here is a snippet of Python code to add a backslash to the end of the directory path:

def add_path_slash(s_path):
    if not s_path:
        return s_path
    if 2 == len(s_path) and ':' == s_path[1]:
        return s_path  # it is just a drive letter
    if s_path[-1] in ('/', '\\'):
        return s_path
    return s_path + '\\'

Upvotes: 0

Ben James
Ben James

Reputation: 125257

"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 then os.path.dirname().

>>> import os
>>> os.path.normpath("c:/foo/bar/")
'c:\\foo\\bar'
>>> os.path.dirname('c:\\foo\\bar')
'c:\\foo'

Upvotes: 5

Related Questions