How to check whether a file exists in Python

preview_player
Показать описание
Don't check, just open it.

How should you check whether a file exists in Python before opening it?

SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)

Want to donate crypto? Check out the rest of my supported donations on my website!

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information, Pi

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Рекомендации по теме
Комментарии
Автор

This is the kind of video I really want to see - straightforward, solid advice on the correct way to do something fundamental in your program.

Alister
Автор

Easier to ask for forgiveness than permission

sy-py
Автор

I've been coding in python for more than 7 years and only recently I took the time to learn to use pathlib. I regret no doing it sooner, it's so much simpler to deal with building and validating path strings, reading and writing to files and building platform agnostic scripts.

baconlife
Автор

There's a reason *not* to use strings, if you are targeting windows, as Paths in Windows may contain unpaired UTF-16 surrogates, which are not representable in UTF-8. That is some Win32 API calls return paths that are UCS-2, which is now obsolete and Windows since Win2000 use UTF-16, but beware that because of Microsofts backwards compatibility stance, you might encounter them.
From MSDN: "There is no need to perform any Unicode normalization on path and file name strings for use by the Windows file I/O API functions because the file system treats path and file names as an opaque sequence of WCHARs."

Ruhrpottpatriot
Автор

Cool video! Simple topic but there is a lot of nuance to a seemingly easy task.

OogaBooga
Автор

For the same reasons, instead of checking if a file exists before writing to it (to avoid overwriting existing data), one should use 'x' mode when opening the file, which will throw exception when opening an existing file. Unfortunately Path.write_text does not allow such possibility.

volodymyrchelnokov
Автор

I dont think you should "Analyze the data" inside the "with" statement because of two reasons:
- You dont need to have the file open in order to process its data, just to read it.
- Processing the file data will take time if it is a large file and that means the file will be open for the entirety of the process.

I think its better to do:

with open(file) as fp:
data = fp.read()
process_data(data)

instead of:

with open(file) as fp:
data = fp.read()
process_data(data)

OmegaMusicYT
Автор

Thank you so much! This was so hard to figure out!

dombomb
Автор

Thank you! Very helpful recommendations!

funkykong
Автор

I am designing some user friendly class methods right now. And having a "string or path like" is exactly what I would to have.
Since there is no goto style guide for developing an API like this ... I am simply following the libraries I am used to using. So I might have a .from_dict or .from_url method.

Veptis
Автор

Great summary, although I would add that you're better off never using strings.

Scymet
Автор

Try/Except is either frowned upon or respected as Pythonic. I probably avoid it more so than I should..

Mutual_Information
Автор

Any chance we see you do a Mojo breakdown?

lukasmrazik
Автор

Had to throw that PEP8 part in there haha. Because come on, who are we to disagree with PEP8!

fartzy
Автор

I saw someone in an open source codebase that's mildly popular try to dodge a CVE with os.path.join. If you pass os.path.join an absolute path all previous arguments are ignored. So you can choose any location, against common programmer expectation.
They opted for simple string concatenation instead.
The same issue exists with Pathlib.

I'm not entirely sure what's the right approach. Maybe just sanetize your input and that's that? But there doesn't seem to be very good ways to enforce this constraint other than constant vigilance.

So I'm unsure about what's a good solution.

xCAFEFD
Автор

Unfortunately, pathlib.Path don't work robust on windows network paths. Not really platform independent. I learned it the hard way.

alcarsharif
Автор

Ruby has a very similar 'duality' in the API i.e. `pathaname` vs `File.* methos`

CAMOBAP
Автор

1:31 doesn't seem to happen on Linux. I think that you need to grab some kind of lock as well if you want that functionality.

MithicSpirit
Автор

Lets assume you Read a Path from a config file? How would you make this work with pathlib instead of a strin?

ali-omuv
Автор

Even though path.exists is a function that, well, exists, I have never once wanted to use it over the dedicated isfile or isdir. Also I really like that Path class/whatever though, I might want to try that out

kalelsoffspring