Python程序未按预期运行

时间:2016-07-05 22:06:16

标签: python linux sqlite

当我运行它时,没有任何反应,但它不会抛出任何错误,它应该做的是提示用户从数据库中提取明矾数字,然后将这些相册从数据库移动到便携式设备。任何想法为什么它没有运行???

   #!/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")

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 处理相册是一个功能,所以要调用它,您需要括号。通过process_albums()替换process_albums。
  2. 您使用过的dbname是process_albums()。您不能使用它,因为它仅在makedatabasename()方法的范围内定义。您可以使用dbname作为全局变量,也可以将其设置为process_albums中的参数。