1

So I scanned my folder then printed it to my text file, but I dont know why it only gives me 1 output, and that output is the last namefile

import zipfile
import os

def out_fun():  
    for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
        print x
    return x
output = out_fun()
file = open("result_vsdt.txt","w")
file.write(output)
file.close()

my only output in my txt file is:

fe8c341de79168a1254154f4e4403857c6e79c46

which has to be:

ed64498d29f40ccc07c7fbfa2a0a05e29763f5e2 ed66e83ae790873fd92fef146a2b70e5597792ee ef2f8d90ebae12c015ffea41252d5e25055b16c6 f4b8b762feb426de46a0d19b86f31173e0e77c2e f4d0cc44a8018c807b9b1865ce2dd70f027d2ceb f6c9b393b5148e45138f724cebf5b1e2fd8d9bc7 fa2229ef95b9e45e881ac27004c2a90f6c6e0947 fac66887402b4ac4a39696f3f8830a6ec34585be fcbbfeb67cd2902de545fb159b0eed7343aeb502 fe5babc1e4f11e205457f2ec616f117fd4f4e326 fe8c341de79168a1254154f4e4403857c6e79c46

2
  • 1
    you iterate across os.listdir and write the restult each time to x, but you return only the last x in the function out_fun(). You should collect all the x in a list and then join it. Commented Sep 25, 2018 at 10:11
  • 1
    You are returning only the last x value (after the end of for loop) from your out_fun. Try returning os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw') instead of x. Commented Sep 25, 2018 at 10:12

3 Answers 3

2

Try this it is a little better, it uses less memory and you don't need to create a list or load all the file content in your function:

import zipfile
import os

def out_fun():  
    for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
        yield x

file = open("result_vsdt.txt","w")
for line in out_fun():
    file.write(line + '\n')
file.close()

Edit:
It is also better to use with when opening file, like this:

with open("result_vsdt.txt","w") as file:
    for line in out_fun():
        file.write(line + '\n')

it will close the file automatically.

Sign up to request clarification or add additional context in comments.

3 Comments

NameError: name 'output' is not defined
@jeremydevera Fixed. it was a typo.
That yield is so important when we have a large list of lines. if you find my answer helpful, please check the green one.
2

Why is only the name of the last file written to the result file?

Because while every value of x gets printed in the out_fun function, only the last one is actually returned. You need to store the others somewhere and return them to.

How to return all contents?

Create a string named output, append every value of x to it and then return it:

import os

def out_fun():  
    output = ''
    for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
        print x
        output += x + '\n'
    return output

with file = open("result_vsdt.txt","w"):
    file.write(out_fun())

'\n' is the new line character. That makes shure, that every filename is on a new line.

2 Comments

UnboundLocalError: local variable 'output' referenced before assignment
I have updated my answer, can you please try it with the full example I provided
0

Your for-loop will iterate through your files and end with the last file. That means, x will always be your last file. If you want to return all files, you need to create a list and save all of them there like:

def out_fun():  
    array = [""]
    for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
        array.append(x)
    return '\n'.join(array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.