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

  • Home
  • SEARCH
  • 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 8901151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:16:35+00:00 2026-06-15T01:16:35+00:00

I have a root-ish directory containing multiple subdirectories, all of which contain a file

  • 0

I have a root-ish directory containing multiple subdirectories, all of which contain a file name data.txt. What I would like to do is write a script that takes in the “root” directory, and then reads through all of the subdirectories and reads every “data.txt” in the subdirectories, and then writes stuff from every data.txt file to an output file.

Here’s a snippet of my code:

import os
import sys
rootdir = sys.argv[1]

with open('output.txt','w') as fout:
    for root, subFolders, files in os.walk(rootdir):
        for file in files:
            if (file == 'data.txt'):
                #print file
                with open(file,'r') as fin:
                    for lines in fin:
                        dosomething()

My dosomething() part — I’ve tested and confirmed for it to work if I am running that part just for one file. I’ve also confirmed that if I tell it to print the file instead (the commented out line) the script prints out ‘data.txt’.

Right now if I run it Python gives me this error:

File "recursive.py", line 11, in <module>
    with open(file,'r') as fin:
IOError: [Errno 2] No such file or directory: 'data.txt'

I’m not sure why it can’t find it — after all, it prints out data.txt if I uncomment the ‘print file’ line. What am I doing incorrectly?

  • 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-15T01:16:37+00:00Added an answer on June 15, 2026 at 1:16 am

    You need to use absolute paths, your file variable is just a local filename without a directory path. The root variable is that path:

    with open('output.txt','w') as fout:
        for root, subFolders, files in os.walk(rootdir):
            if 'data.txt' in files:
                with open(os.path.join(root, 'data.txt'), 'r') as fin:
                    for lines in fin:
                        dosomething()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a root view controller with no nib file, I tried adding this
I have a root UIViewController which has a property called webView. WebView is a
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
I'm trying to install virtual-python on linux server which I don't have root access.
I have an existing large(ish) PHP web app (using Apache and MySQL) which now
I am using a machine on which I do not have root access and
I currently have an svn server running on a machine to which multiple users
I have one activity/java file (Browsefile.java) that would obtain the absolute path of the
I want to know the list of all users who have root (administrative) privilege
I'm doing some C programming on a machine for which I don't have root

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.