Issue
I am new to Python (and Flask) and have a strange problem with strings. Hope someone can tell me why the 2nd code part don't work, when both give the same output with print ? the 2nd part only give blanks in the dropdown list. Thx in advance for your support.
this works:
test=[{'name':'green.tmp'}, {'name':'red.tmp'}, {'name':'blue.tmp'}, {'name':' '}]
count = 0
for character in temp:
count += 1
print(count) # 81
print(test) # [{'name':'green.tmp'}, {'name':'red.tmp'}, {'name':'blue.tmp'}, {'name':' '}]
return render_template('test_index.html', data=test )
this don't work:
path = "/var/lib/mpd/music/"
dirs = os.listdir(path)
temp = "[{'name': '"
for file in dirs:
temp = temp + file + "'}, {'name': '"
temp = temp + " '}]"
count = 0
for character in temp:
count += 1
print(count) # 81
print(temp) # [{'name':'green.tmp'}, {'name':'red.tmp'}, {'name':'blue.tmp'}, {'name':' '}]
return render_template('test_index.html', data=str(temp) )
Solution
Instead of constructing the dict like a string, try this instead.
dirs = os.listdir(path)
temp = []
for dir in dirs:
temp.append({'name': dir})
print(temp)
Answered By - Harshith Bolar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.