为什么没有python运行主要功能?

时间:2014-10-22 23:44:17

标签: python csv main ttl

我是python的新手。我在问为什么python在我的情况下不运行main函数。我在主函数的开头放了一个打印功能但它从不打印。虽然我设法运行其余的代码来在互联网上创建我的html上传表单,所以我设法上传文件。你能帮忙吗?

这部分代码总是运行,不要问我怎么做:(

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()

这部分代码不起作用,至少不与之前的代码一起使用。另外,它确实有效。

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

这是主要的,我把打印到文件的功能永远不会起作用,虽然不知怎的,程序运行第一个函数并生成我的html格式,并允许我上传我想要的文件。奇怪,对吧?

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()

你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

你没有告诉程序写入正确的变量。在主要功能的后半部分将outfile.write更改为Outfile_errors.write