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 594325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:56:29+00:00 2026-05-13T15:56:29+00:00

I have a C++/Obj-C background and I am just discovering Python (been writing it

  • 0

I have a C++/Obj-C background and I am just discovering Python (been writing it for about an hour).
I am writing a script to recursively read the contents of text files in a folder structure.

The problem I have is the code I have written will only work for one folder deep. I can see why in the code (see #hardcoded path), I just don’t know how I can move forward with Python since my experience with it is only brand new.

Python Code:

import os
import sys

rootdir = sys.argv[1]

for root, subFolders, files in os.walk(rootdir):

    for folder in subFolders:
        outfileName = rootdir + "/" + folder + "/py-outfile.txt" # hardcoded path
        folderOut = open( outfileName, 'w' )
        print "outfileName is " + outfileName

        for file in files:
            filePath = rootdir + '/' + file
            f = open( filePath, 'r' )
            toWrite = f.read()
            print "Writing '" + toWrite + "' to" + filePath
            folderOut.write( toWrite )
            f.close()

        folderOut.close()
  • 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-05-13T15:56:29+00:00Added an answer on May 13, 2026 at 3:56 pm

    Make sure you understand the three return values of os.walk:

    for root, subdirs, files in os.walk(rootdir):
    

    has the following meaning:

    • root: Current path which is “walked through”
    • subdirs: Files in root of type directory
    • files: Files in root (not in subdirs) of type other than directory

    And please use os.path.join instead of concatenating with a slash! Your problem is filePath = rootdir + '/' + file – you must concatenate the currently “walked” folder instead of the topmost folder. So that must be filePath = os.path.join(root, file). BTW “file” is a builtin, so you don’t normally use it as variable name.

    Another problem are your loops, which should be like this, for example:

    import os
    import sys
    
    walk_dir = sys.argv[1]
    
    print('walk_dir = ' + walk_dir)
    
    # If your current working directory may change during script execution, it's recommended to
    # immediately convert program arguments to an absolute path. Then the variable root below will
    # be an absolute path as well. Example:
    # walk_dir = os.path.abspath(walk_dir)
    print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
    
    for root, subdirs, files in os.walk(walk_dir):
        print('--\nroot = ' + root)
        list_file_path = os.path.join(root, 'my-directory-list.txt')
        print('list_file_path = ' + list_file_path)
    
        with open(list_file_path, 'wb') as list_file:
            for subdir in subdirs:
                print('\t- subdirectory ' + subdir)
    
            for filename in files:
                file_path = os.path.join(root, filename)
    
                print('\t- file %s (full path: %s)' % (filename, file_path))
    
                with open(file_path, 'rb') as f:
                    f_content = f.read()
                    list_file.write(('The file %s contains:\n' % filename).encode('utf-8'))
                    list_file.write(f_content)
                    list_file.write(b'\n')
    

    If you didn’t know, the with statement for files is a shorthand:

    with open('filename', 'rb') as f:
        dosomething()
    
    # is effectively the same as
    
    f = open('filename', 'rb')
    try:
        dosomething()
    finally:
        f.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just getting started with Obj-C and iOS programming. I have some code that loads
I have several obj-c classes, each of which require a number of constants that
I have an variable obj defined as follows: {user: {username: AzureDiamond, password: hunter2}} I
I have an object obj that is passed into a helper method. public static
I have data from *.obj file. I know how draw with GL_TRIANGLES . How
suppose I have a object: obj:{ child:{ x:12, y:50 }, key1:value1, key2:value2 } if
I have this code: var obj = function (i) { this.a = i; this.init
I have a couple functions like this: object obj.getChild(childIndex) int obj.numChildren() So I am
I have code that looks like this: obj.foo(); // obj might, or might not
I have a function function callback(obj){...} Is it okay to pass in more objects

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.