【发布时间】:2016-07-05 22:06:16
【问题描述】:
当我运行它时,什么也没有发生,但它不会抛出任何错误,它应该做的是提示用户从数据库中提取明矾号码,然后将这些专辑从数据库移动到便携式设备。任何想法为什么它没有运行???
#!/usr/bin/python
import sqlite3
import os
import shutil
get_albums = []
def makedatabase(dbname):
#dbname = 'mp3_database.db'
if not os.path.exists(dbname):
print"Creating new database", dbname, "and tables", "in dir", os.getcwd()
connection = sqlite3.connect(dbname)
cursor = connection.cursor()
cursor.execute("""CREATE TABLE albums (music_location TEXT, genre TEXT, artist TEXT, album TEXT, album_number int)""")
connection.commit()
connection.close()
print"\nDatabase creation complete proceeding to program"
else:
print"Database name already exists!!!!!"
def album_input(selection):
while True:
response = int(raw_input(selection))
get_albums.append(response)
if response == 0:
print 'continuing'
def process_albums():
for num in get_albums:
connection = sqlite3.connect(dbname)
cursor = connection.cursor()
connection.text_factory = str
album_results = cursor.execute("SELECT genre, music_location, artist, album FROM albums where album_number =?", (num,))
(genre, music_location, artist, album) = album_results.fetchone()
connection.commit()
connection.close()
print genre
print album
genre_dir = os.path.join(dest_dir, genre)
artist_dir = os.path.join(genre_dir, artist)
album_dir = os.path.join(artist_dir, album)
if not os.path.isdir(genre_dir):
os.makedirs(genre_dir)
if not os.path.isdir(artist):
os.makedirs(artist)
if not os.path.isdir(album):
os.makedirs(album)
songs = []
for root, dirs, files in os.walk(music_location):
for fname in files:
songs.append(fname)
sorted_songs = songs.sort
for test in sorted_songs:
finished_song = os.path.join(music_location, test)
new_finished_song = os.path.join(artist, test)
shutil.copyfile(finished_song, new_finished_song)
#main
dest_dir = raw_input("Enter the destination directory to move files to: ")
start_database = makedatabase("mp3database.db")
get_selections = album_input("Enter album numbers, Press 0 to process: ")
start = process_albums
finished = raw_input("Press Enter to exit")
【问题讨论】:
-
你需要
process_albums()而不是process_albums -
我修复了这个问题,我还取出了 print 'continuing' 语句并在那里放置了一个 break 语句,现在我得到了全局名称 dbname 没有定义
-
sorted_songs = songs.sort将 sorted_songs 分配给对list.sort方法的引用,即使您正确调用它,您也会将其 sorted_songs 设置为 None,.list.sort()是一个就地方法,所以只是在列表上调用它并遍历列表,否则使用sorted_songs = sorted(songs) -
现在出现 TypeError: 'builtin_function_or_method' object is not iterable
-
你能给我一个如何做list.sort的例子吗,我试过sorted_songs = list.sort(songs)仍然得到NoneType object is not iterable