Skip to main content
Active reading [<https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences> (see also <https://twitter.com/PeterMortensen/status/1199839973215739907>)]. Used more standard formatting (we have italics and bold on this platform). Dressed the naked link. Removed unnecessary formatting.
Source Link
Peter Mortensen
  • 31.6k
  • 22
  • 109
  • 133

TL;DR
answerThe answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough. If file is not exists, it will NOTnot throw any exception.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4Python 3.4, so you need to have Python 3.4+, this lib. This library makes your life much easier while working with files and folders, and it is pretty to use, here. Here is more docdocumentation about it: (https://docs.python.org/3/library/pathlib.html)pathlib — Object-oriented filesystem paths.

BTW, if you are going to reuse the path, then it is better to assign it to a variable.

soSo it will become:

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough. If file is not exists, it will NOT throw any exception.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
The answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough. If file is not exists, it will not throw any exception.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+. This library makes your life much easier while working with files and folders, and it is pretty to use. Here is more documentation about it: pathlib — Object-oriented filesystem paths.

BTW, if you are going to reuse the path, then it is better to assign it to a variable.

So it will become:

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.
added 56 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough. If file is not exists, it will NOT throw any exception.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough. If file is not exists, it will NOT throw any exception.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.
deleted 5 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32

TL;DR
answer is: pathlib module --with a single line of code--

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
answer is: pathlib module --with a single line of code--

Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.

TL;DR
answer is: use the pathlib module


Pathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough.

from pathlib import Path

if Path("myfile.txt").exists(): # works for both file and folders
    # do your cool stuff...

The pathlib module was introduced in Python 3.4, so you need to have Python 3.4+, this lib makes your life much easier while working with files and folders and it is pretty to use, here is more doc about it (https://docs.python.org/3/library/pathlib.html).

BTW, if you are going to reuse the path, then it is better to assign it to a variable

so will become

from pathlib import Path

p = Path("loc/of/myfile.txt")
if p.exists(): # works for both file and folders
    # do stuffs...
#reuse 'p' if needed.
added 3 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
added 26 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
deleted 21 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
added 43 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
added 137 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
edited body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
deleted 48 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
added 10 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
added 8 characters in body
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading
Source Link
Memin
  • 4.1k
  • 1
  • 33
  • 32
Loading