[Tutor] os.path Query
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Feb 7 10:05:06 EST 2021
On 07/02/2021 11:52, John Weller wrote:
> attempt to open it. I have used both os.path.isfile("some_file.txt") and
> os.path.exists("some_file.txt") with apparent success which begs the
> question - what is the difference between them and which should I use?
exists(something)
tells you if something exists in your filesystem.
It gives no clue what something is - a drive, a folder, a file or a
shortcut.
isfile(something)
tells you if something both exists and is a file.
And only a file, anything else will be false.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list