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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:27:24+00:00 2026-06-07T17:27:24+00:00

I am trying to create a text file of the path and file name

  • 0

I am trying to create a text file of the path and file name of everything within a certian directory. After looking around, it looks like the best way to do this is with the GLOB function and I was able to write a code that got me a list of the paths to every file:

import glob
dirlist = glob.glob('P:\MyDep\Myunit\MySection\Stuff\Reports\YR*\O*\*\*\*\*.pdf')
Bureau\Traffic\Reports\YR*\R*\*\*.pdf')
fo=open('new.txt', "w")
fo.write('\n'.join(dirlist))
fo.close()

However, I found myself using excell’s MID statment to extract values from the report. Ideally, I would like the basename of the file inclued with the path as a tuple. I’ve come up with the the following:

for f in glob.glob('P:\MyDep\Myunit\MySection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
        fpath, fname = os.path.split(f)
        rname, extname = os.path.splitext(fname)
        dirtup = (f, rname)
        print dirtup

But I cannot seem to write the results to a text file: the best I can seem to do is generate a text file with one listing from dirtup (edited to show code w/sugestions):

import glob, os
for f in f in glob.glob('P:\MyDep\Myunit\Mysection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    rname, extname = os.path.splitext(fname)
    dirtup = (f, rname)
    fo=open('axlspd.txt', "w")
    fo.write(', '.join(dirtup)+'\n')
    fo.close()

How can I get the all the results of dirtup into a text file? Thanks in advance.

  • 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-07T17:27:27+00:00Added an answer on June 7, 2026 at 5:27 pm

    The problem is that you open the file multiple times. With the ‘w’ flag, the file is truncated each time it is opened, and you would only see the last write.

    Open the file before the loop, and close it after, or use the new-fangled with statement:

    import glob, os
    with open('axlspd.txt', "w") as axlspd:
        for f in f in glob.glob('P:\MyDep\Myunit\Mysection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
            fpath, fname = os.path.split(f)
            rname, extname = os.path.splitext(fname)
            dirtup = (f, rname)
            axlspd.write(', '.join(dirtup)+'\n')
    

    The file is automatically closed when you exit that block of code.


    This is the reference you missed: http://docs.python.org/library/functions.html#open

    The most commonly-used values of mode are ‘r’ for reading, ‘w’ for writing (truncating the file if it already exists)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a text file using VB.Net with UTF8 encoding, without
Cross-post http://perlmonks.org/index.pl?node_id=979710 I'm trying to create a text file from some XML using Perl
I'm trying to create a program that takes a text file of c++ code
Alright well I am trying to create a dictionary from a text file so
I'm trying to create a form that contains these multidimensional arrays: <input type=text name=cost[1][desc]>
I am trying to create a batch file to parse a directory Z:\ (not
I am trying to implement vectorization of a text file...I have created a dictionary
I'm trying to create text in html, that once clicked, the the value of
I am trying to create a text highlight option for my website. But I
I am trying to create full text index but I am getting error which

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.