I have the statement

fn = sys.argv[1]

catching the filename at the command line when the user starts my program. The problem comes when a user uses a path with a space in it such as; 'C:\Documents and Settings\user\My Documents\'.

The argument parser sees ''C:\Documents' as the first paramenter, and 'and Settings\user\My ' as another parameter.

I am trying to get the program to run in the form:

myprogram.py inputfile -1 -2 (two switches). It works as long as the filename and or path have no gaps in them.

I'm using

sys.argv.count("-1")>0:

to parse the switches so I cant surround the filename and path with quoted, because I'm not getting the whole file handed to me.

Any ideas?

Can you just strip off the first and last parameter?

## path_file "".join(sys.argv[1:-1])

import os

test1 = ["program_name.py", "one/path/", "two_paths/", "three", "second_arg"]
path_file = "".join(test1[1:-1])
print path_file
if os.path.isfile(path_file):
    ## etc

I suppose you can, but they have deafults and are not required. If someone runs 'foo.py filename.txt' that's really all they need, and the other parameters drop to default status. plus, in the case ofnetwork files paths, the path can be quite long, and I would have no idea what is and isn't part of the path.

The only thing I can come up with is rather than command line parameters, I use raw_input dialogs to ask for them one at a time. Seem's so 1980.. lol

Use Tkinter's FileDialog (note that you double click a directory to show that directory).

from Tkinter import *
from FileDialog import FileDialog

def get_filename( dir = "" ):
    top = Tk()

    d = FileDialog( top )

    ##---  all arguments to .go are optional
    fname = d.go( dir )
    if fname == None :
        print "No file found", fname
        fname = "****"
    else :
        print "The file is %s" % (fname)

    return fname

##================================================================

if __name__ == '__main__':
    ret_filename = get_filename( "/home/")
    print "filename returned is %s" % (ret_filename)

Use Tkinter's FileDialog (note that you double click a directory to show that directory).

from Tkinter import *
from FileDialog import FileDialog

def get_filename( dir = "" ):
    top = Tk()

    d = FileDialog( top )

    ##---  all arguments to .go are optional
    fname = d.go( dir )
    if fname == None :
        print "No file found", fname
        fname = "****"
    else :
        print "The file is %s" % (fname)

    return fname

##================================================================

if __name__ == '__main__':
    ret_filename = get_filename( "/home/")
    print "filename returned is %s" % (ret_filename)

Thanks for the solution, altho for some reason Tk doesn't run properly on my machine (office PC, winidows XP). When I start it up, I get a Tk panel, and another dialog that has two panels. Closing it hangs up.. I'll take the que though and try it in Wx..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.