Skip to content Skip to sidebar Skip to footer

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

Solution 1:

There are huge swathes of unnecessary code in that function.

def path(request):
    return HttpResponse('\n'.join(os.listdir(request.path_info)))

Job done!

Solution 2:

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.

Solution 3:

I hope the output is like this:

folder1
folder2
folder3
folder4

Thus shall you have your output...

foriinos.listdir(mypath):
    printi

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

Solution 4:

You probably want

a += mess[i]

instead of

a += mess[x]

Solution 5:

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)

Post a Comment for "Why Just One Name Could Taken"