How to check if an argv refers to an existing file in python2.7? [duplicate]
Asked Answered
F

2

7

I know how to check whether required arguments are given on the command line. But how do I check whether the given argument on the command line actually refers to an existing file in the folder I'm running the code in?

I'm trying to incorporate this verification in order to save time by skipping parts of my code in case the file cannot be referenced to.

Flibbertigibbet answered 17/7, 2017 at 14:29 Comment(1)
Read the docs of module os.Obsequent
T
4
import os
os.path.exists(path)

Will return True, if the path exists.

os.path.isfile(path)

Will return True, if the path is a file.

Twinkling answered 17/7, 2017 at 14:31 Comment(0)
T
0

isfile is probably what you are looking for.

from os.path import isfile
from sys import argv
if len(argv) > 1:
    print "is file", isfile(argv[1])
Tasset answered 17/7, 2017 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.