简体   繁体   English

如何从main.py Bottle中调用具有多个功能的现有python文件

[英]How do I call an existing python file with multiple functions from a main.py Bottle

Currently, my code is like this where I upload 2 files but I need to process them in temp files via another existing parse.py file with multiple functions. 当前,我的代码是这样的,我上传了2个文件,但我需要通过另一个具有多种功能的现有parse.py文件在临时文件中对其进行处理。

How can I call them in Templates.py? 如何在Templates.py中称呼它们?

I tried adding import parse.py but it would give an error. 我尝试添加import parse.py,但是会出现错误。

templates.py templates.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

parse.py parse.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()

Just import parse , you don't put .py at the end of an import statement. 只需import parse ,您就不必在导入语句的末尾添加.py Since you seem to want to just use the functions rather than calling parse.clean_up , you could instead do from parse import clean_up . 由于您似乎只想使用函数而不是调用parse.clean_up ,因此可以from parse import clean_up进行from parse import clean_up The file parse needs to either be in your local directory (where you're running the python interpreter) or in your PYTHONPATH environment variable. 文件parse需要在您的本地目录(运行python解释器的目录)中或在PYTHONPATH环境变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM