我写了一个快速简单的hack来遍历目录(在stepmania的歌曲目录中),找到conf文件并将conf文件所在的目录命名为conf文件中的某个名称。这对我的linux系统非常有用。但不是在我的妻子Windows XP中作为管理员运行。我得到许可错误。怎么了?这里是代码:Windows中的路径和权限
#!/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)
哦,这很有道理。我会马上开始编码! :-) –
我做到了。非常感谢! :) –