473,700 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jun 12 '07 #1
8 7824
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

Jun 12 '07 #2

<ky******@gmail .comwrote in message
news:11******** **************@ 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
Jun 12 '07 #3

"T. Crane" <tc****@REMOVET HISuiuc.eduwrot e in message
news:hx******** *******@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.
Jun 12 '07 #4
>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
Jun 12 '07 #5
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
Jun 12 '07 #6
On Jun 12, 9:09 am, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
"T. Crane" <tcr...@REMOVET HISuiuc.eduwrot e in message

news:hx******** *******@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

Jun 12 '07 #7
En Tue, 12 Jun 2007 11:48:32 -0300, <ky******@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

Jun 12 '07 #8

"Gabriel Genellina" <ga*******@yaho o.com.arwrote in message
news:ma******** *************** *************** *@python.org...
En Tue, 12 Jun 2007 11:48:32 -0300, <ky******@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

Jun 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3083
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to look for the first three chars and import rows into separate tables. E.g. if the first three chars begin with CCC, then this row goes into the CCC_table, if it is TTT then into the TTT_table etc... 2. Once I have my tables built up, I need to do...
6
5162
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office programs, Acrobat Reader, some Picture viewer,... ) and be notified when this programs closes the...
11
9594
by: sur | last post by:
Hello, My problem is that File.Exists works fine if my file is on my local drive but returns false if its on any other drive. I think that the issue is probably file permissions and so I have tried the following: FileIOPermision permFileIO = new FileIOPermission(FileIOPermissionAccess.Read, filepath.Value.ToString());
4
4508
by: sunilj20 | last post by:
Hello, I have a requirement wherein, a user clicks on a file name in an ASP.NET web application, and the file should automatically be downloaded (Without showing the "Open", "Save As") in the local machine at a pre defined path (path defined in the registry), and launch the file in its associated application. I think this can be done through ActiveX Controls, but i dont know how to create ActiveX controls in .NET??
5
4331
by: Phil Kelly | last post by:
Hi I need to write the contents of a structure to a binary file - there is one string and 2 integers, but I can't seem to figure out how to write the data correctly. If I am simply writing text to a file there is no problem - that starts when I attempt to write the structure. Can someone help me out, please?
3
3584
by: Johnny | last post by:
Hi, As you know, I can use this to open an Excel file: """ import win32com.client doc = win32com.client.Dispatch("Excel.Application") doc.Workbooks.Open(excelFile, ReadOnly=True) """ But the problem is when I only pass the filename to the Open()
1
64140
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
8
16093
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list which modifies Application setting section but how to add new item to userSettings ? Andrus.
36
5385
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather than retrieve the text into my app or force the user to use another app to read the file. I'm...
0
8642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9066
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2383
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2022
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.