Re: best way to check if a file exists?

2006-11-02 Thread Steven D'Aprano
On Thu, 02 Nov 2006 01:44:25 +1100, Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: Except that there are other conditions than File doesn't

Re: best way to check if a file exists?

2006-11-02 Thread Fredrik Lundh
Steven D'Aprano wrote: But there can be a race condition between os.path.exists returning True and you trying to open the file, if some other process deletes or renames the file in the meantime. if you'd used a threaded newsreader, you would have seen the code that as follows was referring

Re: best way to check if a file exists?

2006-11-01 Thread [EMAIL PROTECTED]
John Salerno wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name.

Re: best way to check if a file exists?

2006-11-01 Thread Ben Finney
[EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: try: f = open('xxx') except IOError: f = open('xxx', 'w') print file doesn't exist print f Except that there are other conditions than File doesn't exist that can cause

Re: best way to check if a file exists?

2006-11-01 Thread [EMAIL PROTECTED]
Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: try: f = open('xxx') except IOError: f = open('xxx', 'w') print file doesn't exist print f Except that there are other conditions than File

Re: best way to check if a file exists?

2006-11-01 Thread Ben Finney
[EMAIL PROTECTED] [EMAIL PROTECTED] writes: Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: Except that there are other conditions than File doesn't exist that can cause an 'open' to fail. Ok, true. You

Re: best way to check if a file exists?

2006-11-01 Thread [EMAIL PROTECTED]
Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: Except that there are other conditions than File doesn't exist that can cause an 'open' to

Re: best way to check if a file exists?

2006-11-01 Thread [EMAIL PROTECTED]
On Nov 1, 3:44 pm, Ben Finney [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: Ben Finney wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: You could try to read the file, if that fails it doesn't exist: Except that there are other conditions than File doesn't

Re: best way to check if a file exists?

2006-11-01 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: os.path.exists() checks if a path exists, so a directory as argument also returns True the original requirement was to check if a file of a certain name exists before the user creates a new file of that name. if you want to check for files only, use the

best way to check if a file exists?

2006-10-31 Thread John Salerno
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name. Thanks. --

Re: best way to check if a file exists?

2006-10-31 Thread utabintarbo
John Salerno wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name.

Re: best way to check if a file exists?

2006-10-31 Thread Gabriel Genellina
At Tuesday 31/10/2006 18:01, John Salerno wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. os.access(full_filename, os.F_OK) http://docs.python.org/lib/os-file-dir.html I just

Re: best way to check if a file exists?

2006-10-31 Thread Michael S
If you want to open the file for writing just open it with append mode. open(¨filename, a). If it doesn't exist - it'll create it, and if it does it'll start appending to the file For reading - os.path.exists(filename). Or (doesn't make much sense, but still) try to open it for reading and

Re: best way to check if a file exists?

2006-10-31 Thread georgeryoung
On Oct 31, 4:01 pm, John Salerno [EMAIL PROTECTED] wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user

Re: best way to check if a file exists?

2006-10-31 Thread Ben Finney
[EMAIL PROTECTED] writes: On Oct 31, 4:01 pm, John Salerno [EMAIL PROTECTED] wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I see that 'os.path.exists' has already been

Re: best way to check if a file exists?

2006-10-31 Thread jim-on-linux
On Tuesday 31 October 2006 16:01, John Salerno wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user