Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if file exists.
#1
I try the following to check if a file exists but when run them, os is highlighted and popup 'invalid syntax' window. not sure why. please advice. thanks.
I use python 3.6.1 on windows 10.

import os

if os.path.exists('test.cvs'):
if os.path.isfile('test.cvs'):
if os.path.islink('test.cvs'):
Reply
#2
Do you try them all in a row like you've shown (and please check out the BBCode link in my signature for info on posting code)? That will cause an IndentationError.

If you are getting a syntax error, I expect you have an unclosed bracket, brace, or parenthesis on the previous line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you. the problem is that I used If not if.
Reply
#4
Don't ask for permission ask for forgiveness.

Instead of checking every possible possibility, you should use try: except:


import sys


filename = 'not_existing_file'

try:
    fd = open(filename)
except OSError as e:
    print(e, file=sys.stderr)
    sys.exit(1)
Or you can be more concrete:

filename = '/dev/mem'

try:
    fd = open(filename)
except PermissionError as e:
    print('You don\'t have the permission to open the file {}'.format(filename), file=sys.stderr)
    sys.exit(1)
except OSError as e:
    # PermissionError is a subclass of OSError
    # First you should be very concrete, and later
    # you can catch all other Errors
    # NEVER USE except:
    # this will catch all Errors
    print(e, file=sys.stderr)
    sys.exit(2)
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Jul-14-2017, 08:22 PM)DeaD_EyE Wrote: Instead of checking every possible possibility, you should use try: except:
I agree with this.
There is a couple of thing that can be done different.
No need to import sys and use file=sys.stderr,just print error give the same.
and use with open().
>>> try:
...     with open('ll.txt') as f:
...         f.read()
... except OSError as error:
...     print(error)
[Errno 2] No such file or directory: 'll.txt'

>>> import sys
>>> try:
...     with open('ll.txt') as f:
...         f.read()
... except OSError as e:
...     print(e, file=sys.stderr)
[Errno 2] No such file or directory: 'll.txt'
Reply
#6
(Jul-14-2017, 08:22 PM)DeaD_EyE Wrote: Instead of checking every possible possibility, you should use try: except:

I don't think this should be taken as a hard and fast rule. The try statement is more efficient if you don't have an error, the if statement is more efficient if you do have an error. If the file is likely to be there you should use try, if it is likely to not be there you should use if.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
If you have to check the file in subfolders too try/except or if statements are not efficient. Both Windows and Linux are using some kind of indexing/database_like so when you ask for a file you get its location in a matter of seconds. 'find' command in Linux does not index or keep info about anything. It's slow too. 'locate' does. If this is the case it's better to use subprocess module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pathlib destpath.exists() true even file does not exist NaN 9 383 Dec-01-2020, 12:43 PM
Last Post: NaN
  Check if a file exists. Pedroski55 5 547 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  How to check to see a dbf file is EOF ? DarkCoder2020 0 382 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 329 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Building a script to check size of file upon creation mightyn00b 2 733 Apr-04-2020, 04:39 AM
Last Post: Larz60+
  Shutil move if file exists in destination Friend 2 856 Feb-02-2020, 01:45 PM
Last Post: Friend
  MySql - Loop - CHeck File gcclinux 1 509 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Sutil.copytree, I'm getting File Exists. Mike Ru 1 662 Oct-28-2019, 07:40 AM
Last Post: Gribouillis
  Check table exists in Database ARV 0 427 Oct-03-2019, 02:37 PM
Last Post: ARV
  how to check for file type in a folder SoulsKeeper 4 1,760 Sep-15-2018, 02:48 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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