Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:24:42+00:00 2026-06-15T09:24:42+00:00

I’m trying to write something that recursively searches a path and subdirectories for files

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:24:43+00:00Added an answer on June 15, 2026 at 9:24 am

    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')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.