I'm trying to write something that recursively searches a path and subdirectories for files that start with the hex value of "FFD8". I have gotten it to work with the location specified in argument parameters when running the script, but the problem comes when it needs to move to a subdirectory.
import string, sys, os
os.chdir(sys.argv[1])
for root, dir, files in os.walk(str(sys.argv[1])):
for fp in files:
f = open(fp, 'rb')
data = f.read(2)
if "ffd8" in data.encode('hex'):
print "%s starts with the offset %s" % (fp, data.encode('hex'))
else:
print "%s starts wit ha different offset" % (fp)
I don't know why I need to use the os.chdir command but for some reason without it, when the script is ran from my desktop it ignores the parameters and always tries to run the search from the desktop directory, regardless what path I specify.
The output from this is autodl2.cfg starts wit ha different offset
DATASS.jpg starts with the offset ffd8
IMG_0958.JPG starts with the offset ffd8
IMG_0963.JPG starts with the offset ffd8
IMG_0963_COPY.jpg starts with the offset ffd8
project.py starts wit ha different offset
Uplay.lnk starts wit ha different offset
Traceback (most recent call last):
File "C:\Users\Frosty\Desktop\EXIF PROJECT\project2.py", line 15, in <module>
f = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'doc.kml'
Now I know the reason WHY it errors here, it is because the file doc.kml is inside a subfolder on the desktop. Can anyone shed somelight on the easiest way to change the directory so that it can continue scanning subdirectories without issue? Thanks!
You need to use absolute filepaths to open them, but files
only lists filenames without a path. However, the root
variable does contain the path to the current directory.
Use os.path.join
to join the two:
for root, dir, files in os.walk(str(sys.argv[1])):
for fp in files:
f = open(os.path.join(root, fp), 'rb')