run a .py script with cmd line args

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r

    run a .py script with cmd line args

    i have a python text editor program(script) that i would like to run
    when a user clicks on .txt files.
    how do i run my script from windows,
    and send the filename to my script, so that my script can do:

    if len(sys.argv) 1:
    try:
    self.LoadFile(s ys.argv[1]) # load text into editor
    except:
    pass

  • Mike Driscoll

    #2
    Re: run a .py script with cmd line args

    On Aug 12, 2:20 pm, r <rt8...@gmail.c omwrote:
    i have a python text editor program(script) that i would like to run
    when a user clicks on .txt files.
    how do i run my script from windows,
    and send the filename to my script, so that my script can do:
    >
    if len(sys.argv) 1:
        try:
             self.LoadFile(s ys.argv[1])  # load text into editor
        except:
            pass

    The following thread explains one way to do it:



    You may also find the mimetypes module helpful:

    Source code: Lib/mimetypes.py The mimetypes module converts between a filename or URL and the MIME type associated with the filename extension. Conversions are provided from filename to MIME type a...


    Mike

    Comment

    • Gabriel Genellina

      #3
      Re: run a .py script with cmd line args

      En Tue, 12 Aug 2008 16:34:13 -0300, Mike Driscoll <kyosohma@gmail .com>
      escribió:
      On Aug 12, 2:20 pm, r <rt8...@gmail.c omwrote:
      >i have a python text editor program(script) that i would like to run
      >when a user clicks on .txt files.
      >how do i run my script from windows,
      >and send the filename to my script, so that my script can do:
      >>
      >if len(sys.argv) 1:
      >    try:
      >         self.LoadFile(s ys.argv[1])  # load text into editor
      >    except:
      >        pass
      >
      >
      The following thread explains one way to do it:
      >
      http://mail.python.org/pipermail/pyt...ch/004360.html
      Instead of directly writing to the Windows registry, you can also execute
      these two commands:

      assoc .foo=Foo.File
      ftype Foo.File=c:\pat h\to\python.exe c:\path\to\scri pt.py "%1" %*

      They work on every non-prehistoric Windows version...

      (replace foo with the desired extension - I would *NOT* use txt!)

      --
      Gabriel Genellina

      Comment

      Working...