views:

287

answers:

2

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!

+5  A: 

"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'
Ben James
I think you are missing the point. the problem is not that bar is at the end, but that python thinks that foo\bar is the name of one directory (because \0 is interpreted as one unit, as far as I understand). Try to make my example work (with a folder starting with a zero), and if you manage, please explain to me how you did it.
AlexandreS
It doesn't have *anything* to do with the directory starting with a 0. The problem is that `os.path.normpath` will remove a trailing slash/backslash, so that subsequently `os.path.dirname` will assume that `090925` is a filename and split before that.
Tim Pietzcker
You are right: if I remove normpath, it works, but only as long as the file is of the format D:\Data2\090925\ If I use D:\Data2\090925, then 090925 is lost.I'll see if I can deal with that
AlexandreS
You can add a backslash. Comments suck for code so I'm adding an answer.
steveha
A: 

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 + '\\'
steveha