使用与每个文件关联的数字而不是文件名来上传文件

时间:2015-04-01 18:05:23

标签: python

我在目录中显示当前文件的列表。

对于每个文件我显示一个关联的数字,然后我想问用户数字3文件,但不是文件名,我想使用相关的数字。

因此,我将每个文件名与dictonary中的数字相关联。

现在我尝试使用与每个文件关联的数字上传,但我没有成功这样做。

有人已经做过这样的事情可以给我一个帮助吗?

import os 

def upload(file_name_number): 

    filename = raw_input(file_name_number)

    if(os.path.exists(filename)):
        key.set_contents_from_filename(filename)
    else:
        print "The selected number does not exist in current directory."
        upload(file_name_number)
    return filename    

def create():

    showListAndSaveDict()

    firstFile  = upload("Select the number of first file:")
    secondFile = upload("Select the number of second file:")
    thirdFile  = upload("Select the number of third file:") 

def showListAndSaveDict():
    files = [f for f in os.listdir('.') if os.path.isfile(f)]
    files_dict = {}
    i=0
    for f in files:
        i = i+1
        print (str(i) + " - " + f)  
        files_dict[i] = f
    return files_dict

create()

2 个答案:

答案 0 :(得分:1)

您正在创建一个字典,用于将数字映射到文件名,但是在上传功能中,您正在检查带有数字的文件是否存在,因为文件存储的是名称而不是数字,所以该文件是错误的。您需要检查该编号是否存在是否存在于字典中。

import os

def upload(file_name_number,files_dict): 

    filename = int(raw_input(file_name_number))
    #as the key to dictionary is integer
    if filename in files_dict:
        # upload code here
        print "dummy"
    else:
        print "The selected number does not exist in current directory."
        upload(file_name_number,files_dict)#why are you calling this again
    return filename

def create():
    files_dict = showListAndSaveDict()
    firstFile  = upload("Select the number of first file:",files_dict)
    secondFile = upload("Select the number of second file:",files_dict)
    thirdFile  = upload("Select the number of third file:",files_dict)

def showListAndSaveDict():
    files = [f for f in os.listdir('.') if os.path.isfile(f)]
    files_dict = {}
    i=0
    for f in files:
        i = i+1
        print (str(i) + " - " + f)  
        files_dict[i] = f
    return files_dict

create()

答案 1 :(得分:1)

你不会在create()中返回任何内容并且你没有通过dict,因此你将无法访问它,所以你可能最好将逻辑移到upload并忘记其他函数使用while循环来保持循环,直到上传三个文件

from pprint import pprint as pp

def upload():
    file_dict = showListAndSaveDict()
    # kepp track of how many successful uploads
    uploaded = 0
    # keep list of all files uploaded
    files_uploaded = []
    # keep looping until three files have been successfully uploaded
    while uploaded < 3:
        for k,v in file_dict.iteritems():
            print("Number {}: file: {}".format(k,v))
        file_number = raw_input("Please choose a file number")
        # make sure choice is valid
        if file_number in file_dict:
            # do whatever
            # increase count 
            uploaded += 1
            files_uploaded.append(file_dict[file_number])
        else:
            # key was not in dict
            print "The selected number does not exist in current directory."
    return files_uploaded


def showListAndSaveDict():
    files = (f for f in os.listdir('.') if os.path.isfile(f))
    return {str(k):"{}-{}".format(k,f)  for k, f in enumerate(files)}

我认为应该有另一个函数实际上传应该调用的文件代替#do whatever

你应该总是使用while循环而不是重复调用自身的函数。