Skip to content Skip to sidebar Skip to footer

Trying To Write Filename To Csv

My code determines if the contents of the file returns True or False and output the results to a .csv. I would like to write the filename also to the csv in the same row. Error m

Solution 1:

You are getting the error because you're trying to use a variable, vt_result_file, that is not in scope--it doesn't exist in the part of the script that you're trying to access it in. You are trying to use it here, presumably to loop over a set of files:

    for i in range(vt_result_file):
        output.write(vt_result_file, vt_result_check(path))

but vt_result_file only exists in the vt_result_check function, which hasn't even been called when you try to use the variable in for i in range(vt_result_file).

You're also duplicating work, as your vt_result_check function iterates through all the files in a directory, so you don't need to do the same thing to get the results.

It looks as though your main function is also not working correctly, as you're iterating through the files, setting the contents to vt_data, but you only do further analysis on the last set of data:

    with open(path + filename, 'r') as vt_result_file:
        # this is done for every file
        vt_data = json.load(vt_result_file)

    # check the indentation level of the code
    # it'll only run on the last value of vt_data
    sample_types = ('detected_referrer_samples', 'detected_communicating_samples',
                    'detected_downloaded_samples', 'detected_urls')
    vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
                                             for sample in vt_data.get(sample_type, []))

I think you probably want to run the analysis code on every file, and then save the result for each file. An easy way to do this is using a dictionary -- see https://docs.python.org/3/library/stdtypes.html#mapping-types-dict Here's a suggestion for how to restructure your code:

def vt_result_check(path):

    # initialise an empty dictionary for your results
    results = {}

    sample_types = ('detected_referrer_samples', 'detected_communicating_samples',
                            'detected_downloaded_samples', 'detected_urls')
    threats = ("elevated exposure", "phishing and other frauds", "suspicious content")

    for filename in os.listdir(path):
        with open(path + filename, 'r') as vt_result_file:
            # set this to false for each file
            vt_result = False
            vt_data = json.load(vt_result_file)

            # do all your analysis here
            vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
                                                     for sample in vt_data.get(sample_type, []))

            # Look for a Dr. Web category of known infection source
            vt_result |= vt_data.get('Dr.Web category') == "known infection source"

            # Look for a Forecepoint ThreatSeeker category of elevated exposure
            # Look for a Forecepoint ThreatSeeker category of phishing and other frauds
            # Look for a Forecepoint ThreatSeeker category of suspicious content
            vt_result |= vt_data.get('Forcepoint ThreatSeeker category') in threats

            # save the file name and the result in your dict
            results[vt_result_file] = str(vt_result)

    # return the dictionary
    return results

You can then print out your results as follows:

if __name__ == '__main__':
    result_dict = vt_result_check(path)
    with open(file_n, 'w') as output:
        for i in result_dict:
            output.write(i + ": " + result_dict[i])

Solution 2:

vt_result_file only exists as a local variable to vt_result_check, your error is saying this variable does not exist at the bottom of the file.

Plus, (even though it doesn't matter) you are referring to that variable before you called the function that creates that variable.


There is nothing you are looping over in the main function area. And your check function only returns a single value.

Therefore, you can only write out one CSV row

if __name__ == '__main__':
    with open(file_n, 'w') as output:
        writer = csv.writer(output)
        writer.writerow([file_n, vt_result_check(path)])

Edit

Regarding your comment, you want something like this

with open(file_n, 'w') as output:  # Open the CSV file
    writer = csv.writer(output)
    for filename in os.listdir(path):  # Loop over all files to check
        full_filename = path + filename
        with open(full_filename, 'r') as vt_result_file: 
            # Load the file and check it 
            vt_data = json.load(vt_result_file)
            writer.writerow([full_filename, check_file(full_filename)])

Post a Comment for "Trying To Write Filename To Csv"