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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:14:34+00:00 2026-06-04T20:14:34+00:00

In Python, I’m writing to a text file with code like: f.write(filename + type

  • 0

In Python, I’m writing to a text file with code like:

f.write(filename + type + size + modified)

And of course the output looks really ugly:

C:/Config/ControlSet/__db.006  file          56 KB   2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet dir       68881 KB   2012-Apr-30 10:00:46.396 AM 
C:/Config/DataStore.avlocate file           0 KB   2012-Apr-30 09:57:42.440 AM 
C:/Config/DataStoreLocate.bak file           0 KB   2012-Apr-30 09:57:42.470 AM 
C:/Config/DeviceConnections/devconn.bak file          41 KB   2012-Apr-30 10:39:50.181 AM   
C:/Config/DeviceConnections/devconn.cfg file          41 KB   2012-May-29 10:12:45.288 AM

But I want to align the entries so it looks like this:

C:/Config/ControlSet/__db.006                                            file          56 KB   2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet                                                      dir       68881 KB   2012-Apr-30 10:00:46.396 AM 
C:/Config/DataStore.avlocate                                             file           0 KB   2012-Apr-30 09:57:42.440 AM 
C:/Config/DataStoreLocate.bak                                            file           0 KB   2012-Apr-30 09:57:42.470 AM 
C:/Config/DeviceConnections/devconn.bak                                  file          41 KB   2012-Apr-30 10:39:50.181 AM   
C:/Config/DeviceConnections/devconn.cfg                                  file          41 KB   2012-May-29 10:12:45.288 AM

My issue is similar to this question except I don’t know how long the filenames will be beforehand. How should I approach this?

  • 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-04T20:14:35+00:00Added an answer on June 4, 2026 at 8:14 pm

    If you can get a list of all filenames first, then you could do something like:

    max_width = max(len(filename) for filename in filenames)
    for filename in filenames:
        f.write(filename.ljust(max_width+1)+..whatever else..)
    

    If you can’t get a list of all filenames first, then there’s no way to make sure that everything will line up, because there’s no way to know if you’ll later get a file whose name is really long.

    In a case like this, though, I would usually just assume that N columns is generally sufficient, for some N, in which case you can just do something like:

    f.write('%-40s %6s %10s %2s\n' % (filename, type, size, modified))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

python newbie here. I'm writing the code to control an experiment that has multiple
Python is installed in a local directory. My directory tree looks like this: (local
Python newbie here. I'm writing a script that can dump some output to either
Python project looks like this: setup.py README Application scripts hello.py shell_scripts date.sh From hello.py
Python beginner >>> import oauth2 Traceback (most recent call last): File , line 1,
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
python manage.py dumpdata modelName > file.json . created an empty database for a user
Python's paster serve app.ini is taking longer than I would like to be ready
Python-Markdown includes features like escaping of raw HTML that are obviously intended to make
Python's file.read() function won't read anything. It always returns '' no matter what's inside

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.