Reputation: 327
def path(request, mypath):
mypath = request.path_info
_listdir = os.listdir(mypath) # ['folder1', 'folder2', 'folder3', 'folder4']
mess = _listdir
a = ' '
x=0
scope = vars()
for i in mess:
scope['x']+=1
a += mess[x]
a += '\n'
return HttpResponse(a)
I hope the output is like this:
folder1 folder2 folder3 folder4
but why the output just like this:
folder1 folder1 folder1 folder1
any help?
Upvotes: 0
Views: 1273
Reputation: 141898
There are huge swathes of unnecessary code in that function.
def path(request):
return HttpResponse('\n'.join(os.listdir(request.path_info)))
Job done!
Upvotes: 4
Reputation: 9942
Most of what you have is unneccesary. You just want to loop through the return values. Not modify them, nor play around with a variable indirectly via scope.
def path(request, mypath):
mypath = request.path_info
dirs = os.listdir(mypath) # ['folder1', 'folder2', 'folder3', 'folder4']
a = ''
for i in dirs:
a += dirs
a += '\n'
return HttpResponse(a)
Upvotes: 1
Reputation: 9162
I hope the output is like this: folder1 folder2 folder3 folder4
Thus shall you have your output...
for i in os.listdir(mypath):
print i
You can return
the i
in the loop with HttpResponse
there should be no problem, do this
returnString = ""
for i in os.listdir(mypath):
returnString = returnString + i + "\n"
return returnString
Upvotes: 1
Reputation: 799150
From the docs:
Note: The returned dictionary should not be modified: the effects on the corresponding symbol table are undefined.
So, don't do that.
Upvotes: 3