我正在尝试将Python脚本here用于我自己的目的。我不是Python的家伙,所以希望有人能看出我的错误。这是缩进错误吗?
Traceback (most recent call last):
File "test.py", line 31, in <module>
csv_doc.write( ouput_to_csv.join("\n") );
NameError: name 'ouput_to_csv' is not defined
其次,此脚本创建CSV。我想跳过这一步,使用MySQLdb将数据直接写入数据库中的表。这可能吗?
# import the standard libraries you'll need
import os # https://docs.python.org/2/library/os.html
import re # https://docs.python.org/2/library/re.html
# this function will walk your directories and output a list of file paths
def getFilePaths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
audio_file_paths = getFilePaths("Z:\Dropbox\Apps\DirScan\files")
output_to_csv = [];
for audio_file in audio_file_paths:
base_path, fname = os.path.split(audio_file)
reg_ex = re.compile("^(.*) - (.*) - (.*).mp3$");
# now apply the compiled regex to each path
name_components = reg_ex.match(fname);
output_to_csv.append("{0},{1}".format(name_components.join(","), base_path));
#create the file, making sure the location is writeable
csv_doc = open("database.csv", "w");
# now join all the rows with line breaks and write the compiled text to the file
csv_doc.write( ouput_to_csv.join("\n") );
#close your new database
csv_doc.close()
背景是我想使用正则表达式将文件夹中的audiofiles导入数据库。格式为Artist - Title - Other info.mp3
答案 0 :(得分:2)
你写ouput_to_csv
必须output_to_csv
你省略t