【问题标题】:Python program not running as expectedPython 程序未按预期运行
【发布时间】: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

标签: python linux sqlite


【解决方案1】:

两件事:

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

【讨论】:

  • 我修复了这两个问题,并更正了排序,但它仍然没有复制文件???
  • 现在可以正确创建目录,只是没有移动文件。此外,它似乎试图将 0 作为数组的一部分处理
  • 如果有人有兴趣,让整个工作正常,现在唯一的问题是它像糖浆一样慢,有什么想法可以让它加速吗?
  • 尝试测试你的函数的速度。由于 sql 查询,它可能会很慢。
  • megadarkfriend 我完全不知道该怎么做,但是将 140 张专辑移动到设备上的速度非常慢,需要 2 小时以上。我真的需要加快这件事。如果你想看,我可以发布整个代码???
猜你喜欢
  • 2022-12-10
  • 2020-01-14
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多