Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Check Whether a File Exists Without Exceptions?

#1
How to Check Whether a File Exists Without Exceptions?

Challenge: Given a string '/path/to/file.py'. How to check whether a file exists at '/path/to/file.py', without using the try and except statements for exception handling?

# What You Want!
if exists('/path/to/file.py'): ... # Do something

Solution: To check whether a file exists at a given path,

  • Run from pathlib import Path to import the path object,
  • Create a path object with Path('/path/to/file.py'), and
  • Run its .is_file() method that returns True if the file exists and False otherwise.
from pathlib import Path if Path('/path/to/file.py').is_file(): print('Yay')

If the file does exist, you’ll enter the if branch, otherwise you don’t enter it. This method works across all operating systems and modern Python versions.

The post How to Check Whether a File Exists Without Exceptions? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...xceptions/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Read a File Without Newlines xSicKxBot 0 20 02-26-2021, 06:41 AM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 25 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] How to Check if an Object is of Type List in Python? xSicKxBot 0 33 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  [Tut] How To Run A File In The IPython Console As Default Instead Of Terminal? xSicKxBot 0 36 01-26-2021, 10:28 AM
Last Post: xSicKxBot
  [Tut] What’s the Standard Way to Check for Type in Python? xSicKxBot 0 32 01-25-2021, 04:47 PM
Last Post: xSicKxBot
  [Tut] How to Read a File Line-By-Line and Store Into a List? xSicKxBot 0 85 10-24-2020, 06:12 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Check if Two Unordered Lists Are Identical xSicKxBot 0 128 06-26-2020, 01:57 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Check if Two Ordered Lists Are Identical xSicKxBot 0 148 06-25-2020, 09:46 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Check if a File Exists in Python xSicKxBot 0 146 06-20-2020, 09:22 AM
Last Post: xSicKxBot
  [Tut] How to Check If a Python List is Empty? xSicKxBot 0 145 06-06-2020, 08:16 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] Best LLC Services for Your Freelan...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 1
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
(Free Game Key) Wargame: Red Dragon - Fr...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
Flutter 2 Release Adds Desktop and Web S...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 3
Microsoft - Meet the 2021 Imagine Cup Wo...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
News - Review: G-Darius HD – A True Shmu...
Last Post: xSicKxBot
Yesterday 07:37 PM
» Replies: 0
» Views: 1
News - Minute Of Islands, The Adventure ...
Last Post: xSicKxBot
Yesterday 07:37 PM
» Replies: 0
» Views: 1
News - Tales From the (Deep Stone) Crypt
Last Post: xSicKxBot
Yesterday 10:30 AM
» Replies: 0
» Views: 6
Xbox Wire - The Ford F-150 RLE Has Arriv...
Last Post: xSicKxBot
Yesterday 10:29 AM
» Replies: 0
» Views: 6
News - Don’t Miss: Designing for seasons...
Last Post: xSicKxBot
Yesterday 10:29 AM
» Replies: 0
» Views: 13

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020