8

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.

Marlen
  • 91
  • 2
  • Read the [docs of module os](https://docs.python.org/2/library/os.html#files-and-directories). – sascha Jul 17 '17 at 14:31

2 Answers2

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.

Maaaaa
  • 388
  • 2
  • 16
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])
SleepProgger
  • 364
  • 6
  • 20