file open default location

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

    file open default location

    Hi,

    How is the default path chosen in this instance:

    myFile = file('test.txt' ,'w')

    Here I'm opening/creating a file but I have not specified the exact path, so
    how does Python determine where to 'put' this file? More to the point, how
    do I change what the default path is? Right now it's a networked drive that
    should not be getting my Python clutter.

    Interestingly, this network drive is also where I can find my _ipython
    folder from my ipython install as well as my .matplotlib folder. Can anyone
    tell me how to change where these folders and files go by default?

    thanks for any help,
    trevis


  • kyosohma@gmail.com

    #2
    Re: file open default location

    On Jun 12, 8:42 am, "T. Crane" <tcr...@REMOVET HISuiuc.eduwrot e:
    Hi,
    >
    How is the default path chosen in this instance:
    >
    myFile = file('test.txt' ,'w')
    >
    Here I'm opening/creating a file but I have not specified the exact path, so
    how does Python determine where to 'put' this file? More to the point, how
    do I change what the default path is? Right now it's a networked drive that
    should not be getting my Python clutter.
    >
    Interestingly, this network drive is also where I can find my _ipython
    folder from my ipython install as well as my .matplotlib folder. Can anyone
    tell me how to change where these folders and files go by default?
    >
    thanks for any help,
    trevis
    When you don't specify where you want to save a file, it saves the
    file to the directory the script itself is run from. As far as I know,
    you have to specify where you want the file saved if you don't want it
    with the script.py

    Mike

    Comment

    • T. Crane

      #3
      Re: file open default location


      <kyosohma@gmail .comwrote in message
      news:1181655985 .895174.116540@ o11g2000prd.goo glegroups.com.. .
      On Jun 12, 8:42 am, "T. Crane" <tcr...@REMOVET HISuiuc.eduwrot e:
      >Hi,
      >>
      >How is the default path chosen in this instance:
      >>
      >myFile = file('test.txt' ,'w')
      >>
      >Here I'm opening/creating a file but I have not specified the exact path,
      >so
      >how does Python determine where to 'put' this file? More to the point,
      >how
      >do I change what the default path is? Right now it's a networked drive
      >that
      >should not be getting my Python clutter.
      >>
      >Interestingl y, this network drive is also where I can find my _ipython
      >folder from my ipython install as well as my .matplotlib folder. Can
      >anyone
      >tell me how to change where these folders and files go by default?
      >>
      >thanks for any help,
      >trevis
      >
      When you don't specify where you want to save a file, it saves the
      file to the directory the script itself is run from. As far as I know,
      you have to specify where you want the file saved if you don't want it
      with the script.py
      Unfortunately, this is not the case. My module's path is this:

      C:\documents and setting\t_crane \my documents\pytho n modules\script. py

      Python is in the directory C:\Python25

      The file is being saved in this path:

      K:\myFile.txt (this is the networked drive mentioned above)

      As an aside, I forgot to mention above that I'm using Windows XP. Any other
      ideas or possible reasons that it would not choose my script location as the
      default location to save something?

      thanks,
      trevis


      Comment

      • Richard Brodie

        #4
        Re: file open default location


        "T. Crane" <tcrane@REMOVET HISuiuc.eduwrot e in message
        news:hxxbi.587$ vi5.192@newssvr 17.news.prodigy .net...
        As an aside, I forgot to mention above that I'm using Windows XP. Any other ideas or
        possible reasons that it would not choose my script location as the default location to
        save something?
        If you open a DOS window and run Python from there, it will write the files
        in whatever directory you were in when you typed the command.

        If you are running Python directly from Windows, or from an IDE, it's up
        to the OS or the IDE to decide what your default directory is. Often it
        will be the home directory from your user profile.


        Comment

        • T. Crane

          #5
          Re: file open default location

          >As an aside, I forgot to mention above that I'm using Windows XP. Any
          >other ideas or possible reasons that it would not choose my script
          >location as the default location to save something?
          >
          If you open a DOS window and run Python from there, it will write the
          files
          in whatever directory you were in when you typed the command.
          >
          If you are running Python directly from Windows, or from an IDE, it's up
          to the OS or the IDE to decide what your default directory is. Often it
          will be the home directory from your user profile.
          I'm using ipython and running everything from there. On my wife's laptop
          the default save location is the home directory of her user profile, but on
          my work computer this is not the case. Therefore I assume that there's some
          setting somewhere that's causing ipython to send stuff to such a weird
          location.

          trevis


          Comment

          • Tim Golden

            #6
            Re: file open default location

            T. Crane wrote:
            myFile = file('test.txt' ,'w')
            >
            Here I'm opening/creating a file but I have not specified the exact path, so
            how does Python determine where to 'put' this file? More to the point, how
            do I change what the default path is? Right now it's a networked drive that
            should not be getting my Python clutter.
            Python doesn't choose anything. Whatever your
            Operating System deems the current drive when
            you start Python is the one which will contain
            any other unqualified files. You can find out
            what it is by running a script which just does:

            import os
            print os.getcwd ()

            and you can change it by doing this:

            import os
            os.chdir ("new-path-of-my-choosing")
            Interestingly, this network drive is also where I can find my _ipython
            folder from my ipython install as well as my .matplotlib folder. Can anyone
            tell me how to change where these folders and files go by default?
            Different question. (And, I'm afraid, a more complicated one). You
            haven't said, but I'm going to guess you're running on Windows,
            not least because any *nix setup I know of will place the user in
            a well-known "Home" directory (typically /home/username).

            The trouble is that applications like ipython, and maybe
            matplotlib, were developed under *nix where you can rely
            on getting hold of a user's "Home" directory either by
            expanding the "~" shell variable -- or whatever it's called --
            or by examining the HOME shell variable. Windows doesn't
            traditionally have either of these things, and has over
            the years had several locations with legitimate claim to
            be "Home".

            Python's own os.expanduser, for example, uses this approach:
            """
            On Windows, only "~" is supported; it is replaced by the
            environment variable HOME or by a combination of
            HOMEDRIVE and HOMEPATH
            """

            I think IPython now uses expandvar. Maybe it always did;
            I've an idea its current behaviour was a more recent
            addition to Python under Windows. But IPython used to
            fall back to C:\ if it couldn't do anything else.
            Don't know about matplotlib. You'll need to check the
            docs (or the source).

            TJG

            Comment

            • kyosohma@gmail.com

              #7
              Re: file open default location

              On Jun 12, 9:09 am, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
              "T. Crane" <tcr...@REMOVET HISuiuc.eduwrot e in message
              >
              news:hxxbi.587$ vi5.192@newssvr 17.news.prodigy .net...
              >
              As an aside, I forgot to mention above that I'm using Windows XP. Any other ideas or
              possible reasons that it would not choose my script location as the default location to
              save something?
              >
              If you open a DOS window and run Python from there, it will write the files
              in whatever directory you were in when you typed the command.
              >
              If you are running Python directly from Windows, or from an IDE, it's up
              to the OS or the IDE to decide what your default directory is. Often it
              will be the home directory from your user profile.
              Oops. My bad. I didn't know that the IDE behaved differently than the
              DOS window.

              Mike

              Comment

              • Gabriel Genellina

                #8
                Re: file open default location

                En Tue, 12 Jun 2007 11:48:32 -0300, <kyosohma@gmail .comescribió:
                On Jun 12, 9:09 am, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
                >If you open a DOS window and run Python from there, it will write the
                >files
                >in whatever directory you were in when you typed the command.
                >>
                >If you are running Python directly from Windows, or from an IDE, it's up
                >to the OS or the IDE to decide what your default directory is. Often it
                >will be the home directory from your user profile.
                >
                Oops. My bad. I didn't know that the IDE behaved differently than the
                DOS window.
                You almost certainly use a shortcut to open the program, either in your
                desktop or Start menu. Right click on it, choose Properties, Shortcut tab,
                and see what it says for "Startup Directory" (or something like that).
                That will be the original "current directory" when the program starts; but
                it may be changed afterwards.

                --
                Gabriel Genellina

                Comment

                • T. Crane

                  #9
                  Re: file open default location


                  "Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrote in message
                  news:mailman.90 18.1181675163.3 2031.python-list@python.org ...
                  En Tue, 12 Jun 2007 11:48:32 -0300, <kyosohma@gmail .comescribió:
                  >On Jun 12, 9:09 am, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
                  >
                  >>If you open a DOS window and run Python from there, it will write the
                  >>files
                  >>in whatever directory you were in when you typed the command.
                  >>>
                  >>If you are running Python directly from Windows, or from an IDE, it's up
                  >>to the OS or the IDE to decide what your default directory is. Often it
                  >>will be the home directory from your user profile.
                  >>
                  >Oops. My bad. I didn't know that the IDE behaved differently than the
                  >DOS window.
                  >
                  You almost certainly use a shortcut to open the program, either in your
                  desktop or Start menu. Right click on it, choose Properties, Shortcut tab,
                  and see what it says for "Startup Directory" (or something like that).
                  That will be the original "current directory" when the program starts; but
                  it may be changed afterwards.
                  AHA! thank you :) I checked the shortcut and NO start up directory was
                  specified, which caused it for some reason to choose the K: drive. After
                  specifying the startup directory it works as I want it to.

                  thanks,
                  trevis


                  --
                  Gabriel Genellina
                  >

                  Comment

                  Working...