I am working on a code that must obtain the directory from the user and produce the following output of each file: Path, FileName, FileSize, and Last Modified Date, and md5 hash.
Here is my code so far:
import os, sys
import stat
import os.path, time
import hashlib
newDirectory = raw_input("Please select a directory to search: ")
while not os.path.exists(newDirectory):
print "Invaid Directory"
newDirectory = raw_input("Please select a directory to search: ")
print "Valid Directory"
dirList = os.listdir(newDirectory)
for file in dirList:
print '\n'
print os.path.abspath(file)
print file
print os.path.getsize(file)
So my output without "print os.path.getsize(file)" prints the path and file name of all the files in the folder. As soon as I add "print os.path.getsize(file)" I get the error message below:
File "C:\Python27\Lib\genericpath.py", line 57, in getsize return os.stat(filename).st_size
WindowsError: [Error 2] The system cannot find the file specified: 'filename'
Clearly, the file exists since it pulls the path and file name. Not sure what I am doing wrong.