newbie pisan
newbie pisan

Reputation: 327

why just one name could taken

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

Answers (5)

johnsyweb
johnsyweb

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

wnoise
wnoise

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

soulseekah
soulseekah

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

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

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

mjhm
mjhm

Reputation: 16705

You probably want

a += mess[i]

instead of

a += mess[x]

Upvotes: 1

Related Questions