Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if a file exists.
#3
As @bowlofred explained - the canonic way is to try to open and handle the exception. Checking if file exists and then try to open it is prone to race condition - the file may be deleted/renamed between your check and your attempt to work with it.

As a side note - it's better using with context manager when open the file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Check if a file exists. - by Pedroski55 - Sep-08-2020, 05:58 AM
RE: Check if a file exists. - by bowlofred - Sep-08-2020, 06:32 AM
RE: Check if a file exists. - by buran - Sep-08-2020, 06:48 AM
RE: Check if a file exists. - by snippsat - Sep-08-2020, 09:38 AM
RE: Check if a file exists. - by perfringo - Sep-08-2020, 09:57 AM
RE: Check if a file exists. - by Pedroski55 - Sep-08-2020, 10:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,320 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  please check this i wanna use a csv file as a graph xCj11 5 2,814 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 3,707 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 3,160 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Check last time file was accessed Pavel_47 4 4,189 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 12,007 Apr-14-2021, 04:21 PM
Last Post: perfringo
  pathlib destpath.exists() true even file does not exist NaN 9 7,383 Dec-01-2020, 12:43 PM
Last Post: NaN
  How to check to see a dbf file is EOF ? DarkCoder2020 0 2,284 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,841 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Building a script to check size of file upon creation mightyn00b 2 3,300 Apr-04-2020, 04:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020