代码之家  ›  专栏  ›  技术社区  ›  peekacode

如何从main调用具有多个函数的现有python文件。py瓶

  •  0
  • peekacode  · 技术社区  · 7 年前

    目前,我的代码是这样的,我上传了2个文件,但我需要通过另一个现有的解析在临时文件中处理它们。具有多个功能的py文件。

    我如何在模板中调用它们。py?

    我尝试添加导入解析。但是它会给出一个错误。

    模板。py公司

    @route('/')
    def index():
    return template('index')
    
    @route('/', method='POST')
    def upload():
    incfile = request.files.get('uploadinc')
    datfile = request.files.get('uploadhex')
    
    macro, ext1 = os.path.splitext(incfile.filename)
    data, ext2 = os.path.splitext(datfile.filename)
    if ext1 not in ('.txt'):
        return 'File extension not allowed.'
    if ext2 not in ('.txt'):
        return 'File extension not allowed.'
    incfile.filename = 'macro.txt'
    datfile.filename = 'data.txt'
    
    curr_dir = os.getcwd()
    print(curr_dir)
    temp_dir = os.path.join(curr_dir, r'temp01')
    if os.path.exists(temp_dir):
       shutil.rmtree(temp_dir)
    os.makedirs(temp_dir)
    
    incfile.save(temp_dir)  
    datfile.save(temp_dir)   
    clean_up(temp_dir)  // gives error
    
    @route('/')
    def clean_up():      // gives error
    

    作语法分析py公司

     import os, sys, re, binascii
    
     def clean_up():
    
        if os.path.exists("dataparse.txt"):
                os.remove("dataparse.txt")
        else:
                print("Creating new files...")
    
        if os.path.exists("out3.txt"):
                os.remove("out3.txt")
        else:
                print("Creating new files...")
    
    def parse_hexdump():
        a = open("data.txt","r")
        b = open("dataparse.txt","a")
        w = open("out3.txt","a")
       str = a.readline()
       w.write(str)
       for line in a:
       if line.startswith('MD') or line.startswith('END OF DISPLAY'):
         continue
       else:
         strline = line[5:40:]  # Slice lines from 5-40 to another file
         b.write(strline+'\n')
      b.close()
      w.close()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   scnerd    7 年前

    只是 import parse ,你不需要 .py 在导入语句的末尾。因为你似乎只想使用函数而不是调用 parse.clean_up ,你可以改为 from parse import clean_up . 文件 parse 需要位于本地目录(运行python解释器的地方)或 PYTHONPATH 环境变量。