Im showing the list of current files in a directory.
And for each file Im showing a number associated, and then I want to ask user to digit 3 file, but not with file name, I want using the associated number.
So Im storing each filename with number associated in a dictonary.
And now Im trying to upload using the number associated to each file, but Im not having sucess doing this.
Someone already did something like this and can give me a help?
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()
You are creating a dictionary for mapping number to file name but in the upload function you are checking whether file with number exists or not which will be false as files are stored with names and not numbers.You need to check whether that number exists in dict or not.
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()
You don't return anything in create()
and you don't pass a dict therefore you won't be able to access it so you may be better off moving the logic to upload
and forget the other functions using a while loop to keep looping until three files are uploaded
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)}
I presume there is supposed to be another function that actually uploads the files which should be call in place of # do whatever
You should always use a while loop instead of the function repeatedly calling itself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.