Forums

Analysis of files

Hi all,

I have a python code that runs perfectly well on my computer, except that my laptop is very slow. I want to upload the code and run it on PythonAnywhere.

I have successfully uploaded some files and my code to my home directory, and the code runs ok. But since I have many files to analyze, I have uploaded a zip file, successfully upzipped it (to home/myname/part1) and saved my python code to the same directory. When I try to run my code in that directory, however, it does not work. In fact, the code does not return any error, it analyzes 2 blank files instead of the 100 or so I uploaded. And then it stops and exits as if the job was done.

Any ideas why? TIA!

Are you using the full path when trying to locate your files? ie /home/andreann/myfolder/myfile.data, not just myfile.data?

I copied my code in home/andreann/part1 and tried running it there (by opening the code and then running it). Here is the code if that helps.

import csv, re from string import punctuation import glob, io

csvfile=open("test.csv", "w", newline='', encoding='cp850', errors='replace') writer=csv.writer(csvfile) for filename in glob.glob('*.txt'):

###Open files and arrange them so that they are ready for pre-processing
with open(filename, encoding='utf-8', errors='ignore') as f:

  analysis

  output=zip(file1, file_name, more_date)
  writer=csv.writer(open('test.csv','a',newline='', encoding='cp850', errors='replace'))
  writer.writerows(output)
  csvfile.flush()

Alternatively, how would I go about extracting the zipped files directly into home/andreann? (That is, so that they are not within a now-unzipped folder)?

You want to make sure the file name is a full path when you call it with open. Alternatively, you could go into the bash console and use mv to move your files.

I think it will all work fine as long as you use full paths. Something like this:

this_folder = os.path.dirname(os.path.abspath(__file__))
for filename in os.listdir(this_folder):
    if filename.endswith('.txt'):
        full_path = os.path.join(this_folder, filename)
        with open(full_path, encoding='utf-8', errors='ignore') as f:
            # etc

Yay, it works! Many, many thanks!