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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:36:46+00:00 2026-06-08T13:36:46+00:00

I’m fairly new to python scripting and I want to verify file names in

  • 0

I’m fairly new to python scripting and I want to verify file names in a directory and subdirectory.
The verification should be case sensitive.
I’m using python 2.6.5
OS: win7 and xp

I prompt for the following user input:

prompt = "year"
year = raw_input(prompt)
prompt = "number"
number = raw_input(prompt)

From here I want to search/verify that the following files and folders exist and their filename is correct.

folderstructure:

..\foobar_(number)_version1\music

Files in subfolder ‘music’

(year)_foobar_(number)_isnice.txt
(year)_itis(number)hot_today.txt
(year)_anything_is(number)possible.txt
(year)_something_{idont_want_to_check_this_part}_(number)_canbe_anything.txt

Note that all text including underscores are always the same, and thus should always be right, except for the things between () or {}.
I want to output the results to a txt file which reports if the filename is correct or not.

What is the most logical method to archieve this?
I’ve read the lib documentation fnmatch(.fnmatchcase), RE and os(.path.isfile) and searched here for examples, but I just can’t figure out where and how to start.

Can anyone point me in the right direction?

[edit]
As soon as my script has the working basics I’ll post my code for reference or to help others.

[edit2] my first non-hello world script

import os
import re

#output :
file_out = "H:\\output.txt"
f_out = open(file_out, 'w')

print "-------start-script----------"

#input
prompt = "enter 4 digit year: "
year = raw_input(prompt)
prompt = "enter 2 digit number: "
number = raw_input(prompt)

print "the chosen year is %s" % (year)
print "the chosen number is %s" % (number)

f_out.write ("start log!\n")
f_out.write ("------------------------------------------\n")
f_out.write ("the chosen year is %s\n" % (year))
f_out.write ("the chosen number is %s\n" % (number))

#part i'm working on

print "end script"
f_out.write ("------------------------------------------\n")
f_out.write ("end script\n")

#close file
f_out.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-06-08T13:36:47+00:00Added an answer on June 8, 2026 at 1:36 pm

    Take a look at the glob module – this will help you get a list of files in the current directory:

    import glob
    
    year = raw_input('Year: ')        # Example: Year: 2009
    number = raw_input('Number: ')    # Example: Number: 12
    filenames = glob.glob('{year}_*{number}*'.format(year=year, number=number))
    

    Filenames will be anything in the current directory that meets the following criteria:

    1. Begins with 2009_
    2. Any number of characters until it matches 12
    3. Any number of characters following 12.

    os.path.exists is a good way to check if the file exists, or os.path.isfile if you want to make sure that it’s really a file and not a directory named like a file. For Python3, check these docs, and like the link ghostbust555 mentioned says, be careful of race conditions if you plan on doing anything besides verifying their existence.


    Based on your comment, it looks like this is a job for regular expressions. The pseudo code for what you need to write looks something like this:

    for filename in list of filenames:
        if filename is not valid:
            print "<filename> is not valid!"
    

    Aside from the actual pattern, the actual python code could look like this:

    import os
    import re
    
    pattern = 'Put your actual pattern here'
    
    # For a different directory, change the . to whatever the directory should be
    for filename in os.listdir('.'):
        if not re.match(pattern, filename):
            print("Bad filename: ", filename)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.