How to execute a program in python script directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scaldo
    New Member
    • Feb 2009
    • 6

    How to execute a program in python script directory

    I am just starting out on Python and trying to make a simple interface as practice. Right now I can execute a program through the os.popen command. However, I have to specify the directory where the program is located. For example, say:

    os.popen("c:\pr ogram^files\pro gramb.py")

    Is there any way I can tell Python to assume the default directory is wherever the interface script is ran from? So say I have a folder and inside are all my python files. So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.). By the way, Python is an AWESOME language, I can't believe I didn't get into this earlier in my life.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Since you know the path to the original script, you can use the os module to split the directory from the file name, and join the directory to any other file name. Example:
    Code:
    >>> fullpath = "c:\\program^files\\programb.py"
    >>> import os
    >>> os.path.splitdrive("c:\\program^files\\programb.py")
    ('c:', '\\program^files\\programb.py')
    >>> os.path.split("c:\\program^files\\programb.py")
    ('c:\\program^files', 'programb.py')
    >>> os.path.join('c:\\program^files', 'newscript.py')
    'c:\\program^files\\newscript.py'
    >>>

    Comment

    • scaldo
      New Member
      • Feb 2009
      • 6

      #3
      Thank you very much for the reply bvdet! I think even though you misunderstood me, you still managed to answer half of my question! What I am asking is my main interface python file and any other python scripts I want to call are all in the same folder. The issue is the folder may not always be in program files; it could be on desktop (which could be something like c:\documents and settings\userna me\desktop), it could be just in c:\, etc. So the directory may change. Is there any way to parse the main interface file location and then use the split technique you showed above to append it to my other python scripts that I might want to call from the main interface file.

      The reason I'm asking this is since I'm going to keep all the python scripts I want in one folder, I might migrate to another desktop environment such as ubuntu, which the file directory wouldn't be the same, or I could move the folder from desktop on my C drive to D drive, etc. Sorry for being misleading; it was difficult for me to frame the question too...

      Comment

      • kaarthikeyapreyan
        New Member
        • Apr 2007
        • 106

        #4
        check if this is wat is required ?

        So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.)
        The path for the file that is going to run has to be either hardcoded or must be obtained from the runtime from a user or process, either of the ways you can get where the file is working from, then you could very well use the os.chdir(<PATH obtained during runtime>) and execute the other scripts or set the PATH to a variable and access it later.

        Comment

        • pythonner
          New Member
          • Aug 2007
          • 11

          #5
          Use the sys module

          The way I have done this is to use the sys module, like so:

          Code:
          import sys
          path = sys.path[0]
          print path
          The current path will be printed out.

          Comment

          • scaldo
            New Member
            • Feb 2009
            • 6

            #6
            Thanks everyone! I will try and see what I can do. However, I have encountered a second problem. The os.popen command with the file location given does not seem to work at all in Ubuntu. For instance, if I just have

            os.popen('/home/scaldo/Desktop/somefile.txt')

            does not work. Even if I use chmod +x somefile.txt and I know the file location is correct.I am new at Ubuntu, and the Linux world in general. Is there something I am missing (I feel I am...)?

            Comment

            • pythonner
              New Member
              • Aug 2007
              • 11

              #7
              Originally posted by scaldo
              Thanks everyone! I will try and see what I can do. However, I have encountered a second problem. The os.popen command with the file location given does not seem to work at all in Ubuntu.

              os.popen('/home/scaldo/Desktop/somefile.txt')
              What are you trying to do with this? I think os.popen is for executing programs, not for opening text files.

              Comment

              • scaldo
                New Member
                • Feb 2009
                • 6

                #8
                Originally posted by pythonner
                What are you trying to do with this? I think os.popen is for executing programs, not for opening text files.
                I'm just testing waters with the features in Python. So the program will be able to open other programs as well as open files (such as text files). I was looking into this, and the other one to my knowledge would be the os.system, but neither one worked. Basically I just want to know the commands to open other programs as well as files. I've tried looking online but could not find one that would work. It works in Windows, but Ubuntu is a different beast altogether, so having a hard time porting over my code in Windows into Ubuntu.

                Comment

                • pythonner
                  New Member
                  • Aug 2007
                  • 11

                  #9
                  I have been dealing with the same issue. I'm also a python noob, but a couple of things comes to mind: remember, Windows uses a backward slash, '\', and linux uses a forward slash, '/'.

                  Also, to have your python script use the current path, use:

                  Code:
                  import sys
                  path = sys.path[0]
                  path will be the current path.

                  Comment

                  • scaldo
                    New Member
                    • Feb 2009
                    • 6

                    #10
                    Thanks for the reply again. Your method does indeed work for my situation; however, I don't know how to implement it. Like I've said above, I really only know the system and popen commands to open/execute a program or file. How would I use the correct syntax to make this work? Like what I have now would be result = os.system(path/somefile.txt), but that doesn't work. Sorry for the newbieness... just having a hard time grasping all the finer details of Python.

                    Comment

                    • pythonner
                      New Member
                      • Aug 2007
                      • 11

                      #11
                      to open a file for reading:

                      Code:
                      file = open("c:\path\to\file.txt", 'r')  # Windows
                      file = open("/home/user/file.txt", 'r')  # for Linux
                      for line in file:
                          print line,
                      file.close()

                      This will print out each line of "file".

                      Comment

                      • scaldo
                        New Member
                        • Feb 2009
                        • 6

                        #12
                        Thanks again. Is there any way I can open the file using another program, but it's called by the Python script? For example, if I want to use my Python script to open a text file using gedit or a pdf file using document viewer. Because ultimately I want to leave the terminal and have as much "GUI" as possible. Is there a generic way to open a file that may be handled by another program (such as, again, gedit for text or document viewer for pdf). Nevertheless, I really do appreciate the help, it's been a tremendous boost to what I would otherwise be in a big headache.

                        Comment

                        • pythonner
                          New Member
                          • Aug 2007
                          • 11

                          #13
                          Sorry, I haven't crossed that bridge yet. Try asking on the python tutor mailing list.

                          Comment

                          • boxfish
                            Recognized Expert Contributor
                            • Mar 2008
                            • 469

                            #14
                            I'm not sure I completely understand your question, but would it work to use an operating system specific command through the os.system function?
                            Code:
                            import os
                            os.system("'C:/myprogram' 'C:/myfile.txt'")

                            Comment

                            Working...