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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:02:42+00:00 2026-05-25T16:02:42+00:00

I have a folder containing my books that are in various formats (.pdf, .djvu,

  • 0

I have a folder containing my books that are in various formats (.pdf, .djvu, .dvi). and they all follow the format:

[Name of the Book] – [Author].[an identifier indicating if it searchable or not].[filetype]

I want to make a list of my books that is of the format (x,y,z,t) where x is the name of the book, y is the author, etc. My problem is that when I do:

for file in os.listdir('/home/username/Books'):

file is a string, thus immutable, so I cannot change it.

  • 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-25T16:02:43+00:00Added an answer on May 25, 2026 at 4:02 pm

    Strings are immutable, but that doesn’t mean you can’t create the tuple you need from the string.

    Something like this should work:

    def file_to_tuple(file):
        title_author, searchable, ext = file.rsplit('.', 2)
        title, author = title_author.rsplit(' - ', 1)
        return (title, author, searchable, ext)
    

    You can then use this in a variety of ways to convert your file list to a list of tuples, here are a couple of options:

    book_list = map(file_to_tuple, os.listdir('/home/username/Books'))
    
    book_list = [file_to_tuple(f) for f in os.listdir('/home/username/Books')]
    

    str.rsplit() with the maxsplit param is used so that it will not fail for titles that contain a period or a dash, or authors that contain a period, for example:

    >>> file_to_tuple('Narnia - The Silver Chair - C.S. Lewis.1.pdf')
    ('Narnia - The Silver Chair', 'C.S. Lewis', '1', 'pdf')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a secured folder containing secret report files (in pdf format). Each
I have a folder containing over 200 raw images, i want to convert all
I have a bash script set that executes in a folder containing a number
I have an asp.net application containing pages that lie in multiple folder. I have
I have a folder.ini containing: [Labels] ; Ordnername name=testverzeichnis ; Ordnerbeschreibung description=verzeichnis zu testen
I have a php script that steps through a folder containing tab delimited files,
i want to download folder containing files like pdf,jpg,png etc from web services.I have
I have a folder containing thousand of html files. I want initially all these
In my Rails 3.2 models directory, I have a folder foo containing two classes:
I have a zip file containing a folder and inside the folder I have

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.