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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:36:00+00:00 2026-06-14T21:36:00+00:00

I want to automate the file name used when saving a spreadsheet using xlwt

  • 0

I want to automate the file name used when saving a spreadsheet using xlwt. Say there is a sub directory named Data in the folder the python program is running. I want the program to count the number of files in that folder (# = n). Then the filename must end in (n+1). If there are 0 files in the folder, the filename must be Trial_1.xls. This file must be saved in that sub directory.

I know the following:

import xlwt, os, os.path
n = len([name for name in os.listdir('.') if os.path.isfile(name)])

counts the number of files in the same folder.

a = n + 1
filename = "Trial_" + "a" + ".xls"
book.save(filename)

this will save the file properly named in to the same folder.

My question is how do I extend this in to a sub directory? Thanks.

  • 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-14T21:36:01+00:00Added an answer on June 14, 2026 at 9:36 pm

    os.listdir('.') the . in this points to the directory from where the file is executed. Change the . to point to the subdirectory you are interested in.

    You should give it the full path name from the root of your file system; otherwise it will be relative to the directory from where the script is executed. This might not be what you want; especially if you need to refer to the sub directory from another program.

    You also need to provide the full path to the filename variable; which would include the sub directory.

    To make life easier, just set the full path to a variable and refer to it when needed.

    TARGET_DIR = '/home/me/projects/data/'
    n = sum(1 for f in os.listdir(TARGET_DIR) if os.path.isfile(os.path.join(TARGET_DIR, f)))
    new_name = "{}Trial_{}.xls".format(TARGET_DIR,n+1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vendor supplied MSI file, and we want to automate the install
I want to Automate the build process of my company. We are Using.Net platform
I need to generate a PKCS12 file using Java. Actually, I need to automate
I want to insert data from included file and create HTML-elements automatically. In data.php
I want to automate a program that reads a file, processes it and then
I want to automate Excel using Perl to do the following task(s): For a
I want to update some file in a server, with an automated script and
I want to automate a MS-DOS command which requires a password, however the password
The problem is following: I want to automate the way my emacs starts. It
I want to be able to automate this so I can go to one

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.