By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

search to see if a file exists

ironmonkey69
P: 43
Can someone help me with a python script that can see if a file exists and to return a number if does or doesn't?
Aug 17 '07 #1
Share this Question
Share on Google+
7 Replies


ilikepython
Expert 100+
P: 844
Can someone help me with a python script that can see if a file exists and to return a number if does or doesn't?
Have you seen the os module? Try the os.phat.exists() function.
Aug 17 '07 #2

bartonc
Expert 5K+
P: 6,596
Can someone help me with a python script that can see if a file exists and to return a number if does or doesn't?
Actually in os.path module:
exists( path)

Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
Aug 18 '07 #3

ilikepython
Expert 100+
P: 844
Actually in os.path module:
Isn't that what the O/P wants? It return true if the path exists or false if it doesn't, right? That's how it works in my computer.
Aug 19 '07 #4

bartonc
Expert 5K+
P: 6,596
Isn't that what the O/P wants? It return true if the path exists or false if it doesn't, right? That's how it works in my computer.
Yep:
Have you seen the os module? Try the os.phat.exists() function.
I was correcting the misspelling and pointing out the fact that os.path is documented separately.
Aug 19 '07 #5

ilikepython
Expert 100+
P: 844
Yep:I was correcting the misspelling and pointing out the fact that os.path is documented separately.
Oh, ok, I got you .
Aug 19 '07 #6

P: 11
I've tried to use import os.path and then use "exists", but I get NameError: "NameError: name 'exists' is not defined"

As the path I wrote the file name itself, since it's located as the same directory as the script: exists(FileName)

Does anyone has an idea what is the problem here?
Aug 19 '07 #7

ilikepython
Expert 100+
P: 844
I've tried to use import os.path and then use "exists", but I get NameError: "NameError: name 'exists' is not defined"

As the path I wrote the file name itself, since it's located as the same directory as the script: exists(FileName)

Does anyone has an idea what is the problem here?
Either do:
Expand|Select|Wrap|Line Numbers
  1. from os.path import exists
  2. exists(FileName);
  3.  
or do:
Expand|Select|Wrap|Line Numbers
  1. import os.path
  2. os.path.exists(FileName)
  3.  
Aug 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.