3 Replies - 483 Views - Last Post: 10 March 2012 - 10:38 PM Rate Topic: -----

#1 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Reading File... from Where?

Posted 10 March 2012 - 07:41 PM

I have this code:

#printfile.py

def main():
    fname = raw_input("Enter filename: ")
    infile = open(fname, 'r')
    data = infile.read()
    print data

main()    



I have this file saved to the Desktop. If the user inputs file.txt during execution, where exactly does Python look to open that file?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading File... from Where?

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Reading File... from Where?

Posted 10 March 2012 - 08:57 PM

It'll check the current working directory :)

Let me give you an example. let's say I don't have Python in my path (sue me, I don't) so I open up a prompt and I navigate to C:\Python27\ and typed in:

python C:\Users\Adam\Desktop\printfile.py

in this situation, Python would look for that file from C:\Python27\ because that is my current working directory.

On the other hand.

Let's say I navigate to C:\Users\Adam\Desktop\ and then ran this:

C:\Python31\python printfile.py

in this situation, it would look for the file in C:\Users\Adam\Desktop\ because that is my current directory.

EDIT:
Well wait a minute, let's say you have your pc set to run automatically when you double click it. Well, in that case it'll check your desktop. Here's a little script I wrote that you can use to confirm it. This script will print out the current working directory, which is where it'll look.

import os
input(os.getcwd())



I'm assuming you use Linux or Windows.

This post has been edited by atraub: 10 March 2012 - 09:12 PM

Was This Post Helpful? 1
  • +
  • -

#3 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Re: Reading File... from Where?

Posted 10 March 2012 - 09:48 PM

View Postatraub, on 10 March 2012 - 08:57 PM, said:

It'll check the current working directory :)

Let me give you an example. let's say I don't have Python in my path (sue me, I don't) so I open up a prompt and I navigate to C:\Python27\ and typed in:

python C:\Users\Adam\Desktop\printfile.py

in this situation, Python would look for that file from C:\Python27\ because that is my current working directory.

On the other hand.

Let's say I navigate to C:\Users\Adam\Desktop\ and then ran this:

C:\Python31\python printfile.py

in this situation, it would look for the file in C:\Users\Adam\Desktop\ because that is my current directory.

EDIT:
Well wait a minute, let's say you have your pc set to run automatically when you double click it. Well, in that case it'll check your desktop. Here's a little script I wrote that you can use to confirm it. This script will print out the current working directory, which is where it'll look.

import os
input(os.getcwd())



I'm assuming you use Linux or Windows.


I tried testing out both scenarios, and I get it now. Thanks! Also, does it make a difference whether I execute the script in the command prompt or in IDLE?
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Reading File... from Where?

Posted 10 March 2012 - 10:38 PM

It shouldn't. Using that little script I gave you, you could test it on IDLE ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1