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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:21:50+00:00 2026-05-26T21:21:50+00:00

I wrote a fast and easy hack to walk thru directories (in stepmania song

  • 0

I wrote a fast and easy hack to walk thru directories (in stepmania song dir), find conf-files and name the directory the conf-files are in to a certain name found in the conf-file. This works great on my linux box. But not at my wives Windows XP-box running as an admin. I get permission-error. What’s wrong? Here’s the code:



#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import with_statement

import os
import re
import sys

def renamer(in_path):
    for (path, dirs, files) in os.walk(in_path):
        exts = ['.sm', '.dwi'] # Only search files with this suffix
        conf_files = [] 

        # Create list with conf-files
        for ext in exts:
            conf_files.extend([file for file in files if file.lower().endswith(ext)])

        # Search for conf-files in directory 
        for conf_file in conf_files:
            try:
                with open(os.path.join(path, conf_file)) as f:
                    match = re.search('TITLE:\s?(.*);', f.read()) # Search for whatever follows "TITLE:"
                    new_dir_name = match.group(1) # The new dir-name is whatever the TITLE states in conf-file
                    os.rename(path, os.path.join(path, '..', new_dir_name)) 
            except IndexError:
                print 'No conf-file in', path

if __name__ == '__main__':
    path = sys.argv[1].replace('\\', '/') # Windowsify the path
    renamer(path)


  • 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-26T21:21:50+00:00Added an answer on May 26, 2026 at 9:21 pm

    Windows can’t rename a path that has an open file. It should work if you move the os.rename call out of the with block so that the file is closed. However, you’re repeating this for multiple files in the same path, and the directory name in path will no longer exist after you’ve renamed it. Also, os.walk can’t traverse subdirectories after you’ve renamed the parent directory.

    I would check the config files while walking the tree and append (path, new_path) tuples to a list. Then I’d rename the directories in reverse order.

    Also, match might be None, in which case trying to access match.group will raise an AttributeError. And Windows system calls seem to handle mixed separators fine in case you want to skip the ‘Windowsify’ step. To clean up a path for printing/logging, os.path.normpath consistently uses os.path.sep as well as resolving ‘.’ and ‘..’ in a path.

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

Sidebar

Related Questions

Which is the fast way to write text file in java? At the moment
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a symfony task to fill a database of sample data. Here's a
I wrote a process explorer using C with GUI interface. I want to add
I wrote some script in a site. The script makes a new spreadsheet and
I wrote a simple XML file and a DTD file including an entity, but
I wrote a collada loader for my model viewer. Write now it loops through
I wrote a C++ DLL and it has been compiled. It requires Visual C++
I wrote a PHP script that retrieves values from a MySQL Query. I used
I wrote a Flickr search engine that makes a call to either a public

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.