Sir.Lich Sir.Lich -4 years ago 214
Python Question

os.path.isFile(file) throwing error in Python 2.7

Since my code is so simple, I will include it in its entirety:

#!/usr/bin/python
import sys
import os.path
def main(argv):
if len(sys.argv) < 3:
print "insuficient arguments. Use like: lichcrypt <d/e> </path/to/file>"
else:
operation = argv[1]
filepath = argv[2]
print operation
print os.path.isFile(filepath)
main(sys.argv)


The error, when run script-style from cmd line is:

liam@liam-GA-970A-UD3:~$ lichcrypt.py something not/a/file/path
something
Traceback (most recent call last):
File "/home/liam/bin/lichcrypt.py", line 12, in <module>
main(sys.argv)
File "/home/liam/bin/lichcrypt.py", line 11, in main
print os.path.isFile(filepath)
AttributeError: 'module' object has no attribute 'isFile'


Now, this code is obviously very useless. Eventually it will encrypt files, but untill I can get my hands on the file itself it can't do anything.

Any thoughts on why os.filepath.isFile() does not seem to exist anymore?

Answer Source

The function names in python are case sensitive, the function is isfile not isFile .

reference link : os.path.isfile

#!/usr/bin/python
import sys
import os.path
def main(argv):
    if len(sys.argv) < 3:
        print "insuficient arguments. Use like: lichcrypt <d/e> </path/to/file>"
    else:
        operation = argv[1]
        filepath = argv[2]
        print operation
        print os.path.isfile(filepath)
main(sys.argv)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download