-1

I am quite a newby with python. I am asking why python does not run the main function in my case. I put a printing function at the beginning of the main function but it never prints. Although I manage to run the rest of the code to create my html upload form on the internet so I do manage to upload files on it. Can you please help?

This part of the code always runs,dont ask me how :(

import web
import csv

urls = ('/upload', 'Upload')

class Upload:
    def GET(self):
        web.header("Content-Type","text/html; charset=utf-8")
        return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file"  name="myfile" />

<br/>
<input type="submit" />
</form>
</body></html>"""

    def POST(self):
        x = web.input(myfile={})

        outfile_errors = open('C:\Python27\csv_results\errors.csv', 'a')
        outfile_errors.write("in the post " + "\n " + str(x) + "\n " )

        filedir = "C:\Python27\csv_results\\"# change this to the directory you want to store the file in.
        if 'myfile' in x: # to check if the file-object is created

            filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.

            outfile_denumire_fisier = open('C:\Python27\csv_results\denumire_fisier.csv', 'a')
            outfile_denumire_fisier.write(filepath + ";" )
            outfile_denumire_fisier.close()

            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored
            fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
            fout.close() # closes the file, upload complete.


        raise web.seeother('/upload')
        #transform()
        outfile_errors.close()

This part of the code doesnt work, at least not together with the previous. Separately, it does work.

def transform():
        outfile_errors = open('C:\Python27\csv_results\errors.txt', 'a')
        outfile_errors.write("in the transform ")
        outfile_errors.close()
#bla bla

This is the main, where i put a printing-to-file function that never works, although somehow the program runs the first function and generates my html format and allows me to upload files like i want. strange, right?

if __name__ == "__main__":
   outfile_errors = open('C:\Python27\csv_results\errors.csv', 'a')
   outfile_errors.write("in the main beginning" + "\n " )
   app = web.application(urls, globals()) 
   app.run()
   outfile_errors.write("in the main middle" + "\n " )
   transform()
   outfile_errors.write("in the main end " + "\n " )
   outfile_errors.close()

Can you please help?

kaileena
  • 119
  • 9

1 Answers1

1

you are not telling the program to write to the correct variable. Change outfile.write to Outfile_errors.write in the second half of your main fucntion

Kevin Swim
  • 17
  • 5