Previous in Forum: Looking for a Simple Project Management and Tracking Tool   Next in Forum: PLC Programming and Network Help
Close
Close
Close
11 comments
Rate Comments: Nested
Active Contributor

Join Date: Dec 2011
Location: banglore
Posts: 18

Python Programming Question

06/20/2013 8:26 AM

How to check if FileExists or not in python?

Note:!!!!!!!!!!!!!!!! file name starts with '\txxxx.txt'

i tried using os.path.exists()

but this did work :(

Any quick anwers are appreciated

Register to Reply
Pathfinder Tags: Python
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Guru

Join Date: Oct 2008
Posts: 42355
Good Answers: 1693
#1

Re: Python Programming Question

06/20/2013 8:55 AM
Register to Reply
Active Contributor

Join Date: Dec 2011
Location: banglore
Posts: 18
#8
In reply to #1

Re: Python Programming Question

06/26/2013 8:54 AM

I wanted a special case where file names start with '\t***'.

Register to Reply
4
Member

Join Date: Jun 2012
Posts: 9
Good Answers: 1
#2

Re: Python Programming Question

06/20/2013 9:08 AM

How did you use it? Code sample? Operating system?

I have the following working in Windows 7 just fine (pretend the underscores are indentations, CR4 keeps stripping out my whitespace):

if not os.path.exists("errors.txt"):
__createdFile = open("errors.txt", "w")
__createdFile.write("")
__createdFile.close()
__print " -created errors.txt"
else:

__print " -errors.txt already exists"

Register to Reply Good Answer (Score 4)
Guru
Technical Fields - Technical Writing - New Member Engineering Fields - Piping Design Engineering - New Member

Join Date: May 2009
Location: Richland, WA, USA
Posts: 21017
Good Answers: 795
#3

Re: Python Programming Question

06/20/2013 10:17 AM

Where's "banglore"?

__________________
In vino veritas; in cervisia carmen; in aqua E. coli.
Register to Reply
Guru
United Kingdom - Member - Indeterminate Engineering Fields - Control Engineering - New Member

Join Date: Jan 2007
Location: In the bothy, 7 chains down the line from Dodman's Lane level crossing, in the nation formerly known as Great Britain. Kettle's on.
Posts: 32175
Good Answers: 839
#5
In reply to #3

Re: Python Programming Question

06/20/2013 11:09 AM

One might need to be a visionary........never mind.

__________________
"Did you get my e-mail?" - "The biggest problem in communication is the illusion that it has taken place" - George Bernard Shaw, 1856
Register to Reply
Active Contributor

Join Date: Dec 2011
Location: banglore
Posts: 18
#9
In reply to #3

Re: Python Programming Question

06/26/2013 8:55 AM

It is in India (state : Karnataka)..

Register to Reply
Active Contributor

Join Date: Dec 2011
Location: banglore
Posts: 18
#11
In reply to #3

Re: Python Programming Question

06/27/2013 3:30 AM
It is in India (state : Karnataka)..
Register to Reply
3
Participant

Join Date: Jun 2013
Posts: 1
Good Answers: 1
#4

Re: Python Programming Question

06/20/2013 10:33 AM

This works for me:

Register to Reply Good Answer (Score 3)
Anonymous Poster #1
#10
In reply to #4

Re: Python Programming Question

06/26/2013 10:32 PM

if the \ is to be included in the string, use:

r'\t*.txt'

or

'\\t*.txt'

otherwise you will have a tab character in the string.

Register to Reply Score 1 for Good Answer
Associate

Join Date: Jun 2009
Location: Minneapolis, MN
Posts: 47
Good Answers: 5
#6

Re: Python Programming Question

06/20/2013 10:24 PM

on stackoverflow.com I got 7,996 results from "python if file exists"

__________________
There's ALWAYS another plan.
Register to Reply
Active Contributor

Join Date: Dec 2011
Location: banglore
Posts: 18
#7

Re: Python Programming Question

06/26/2013 8:53 AM

Thank you all for your great answers it really helped me :)

Register to Reply
Register to Reply 11 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

d_entrepreneur (1); jweis (1); lyn (1); PWSlack (1); Tornado (1); viresh_cr4 (4); wjuffras (1)

Previous in Forum: Looking for a Simple Project Management and Tracking Tool   Next in Forum: PLC Programming and Network Help

Advertisement