input() 在 Python 中无法被 main 中的其他模块识别



我有两个脚本,一个是 main.py 的,它正在通过终端运行并接受input((。script1.py 基本上会接收文件并对其进行解析。

由于导入 script1.py 说变量未定义,我一直收到错误。 错误:

File "/Users/jeremy/Desktop/folder1/script1.py", line 5, in <module>
open_document= open(document_path)
NameError: name 'document_path' is not defined``` 

main.py:

import sys
import os
import csv
import pandas as pd
from maybe import start_maybe 
from script1 import start_s1
document_path = input("What is the file path? ")
open_document = open(document_path) #filelocation 
good = input("What is/are the good numbers? ")
bad = input("What is/are the bad numbers? ")
function1 = start_s1(good,bad)
print ("FQDN document created!")
maybeasn = start_maybe(good,bad)
print("Directory created for manual review, file is located inside")

这是 script1.py:

import os
import csv
import re
import main.document_path
open_document= open(document_path)
file_name = (os.path.basename(document_path))
def start_s1(good, bad):
with open ((('fqdn_') +(str(file_name).rstrip('.csv'))) , 'w') as output:
with open_document as file:
fqdn_data = csv.writer(output)
reader = csv.reader(file)
good_nums = good
bad_nums = bad
maybe_nums = []
for row in reader:
if row[3] in good_nums:
fqdn_data.writerow([row[2]])

我做错了什么以及修复方法是什么,以便 script1.py 可以了解我的变量document_path来自 main.py

任何帮助将不胜感激。

根据我的评论,您应该重组代码并在模块/函数之间传递 documentpath 变量。

main.py

import sys
import os
import csv
import pandas as pd
from maybe import start_maybe 
from script1 import start_s1
document_path = input("What is the file path? ")
good = input("What is/are the good numbers? ")
bad = input("What is/are the bad numbers? ")
function1 = start_s1(document_path,good,bad)
print ("FQDN document created!")
maybeasn = start_maybe(good,bad)
print("Directory created for manual review, file is located inside")

script1.py:

import os
import csv
import re

def start_s1(document_path,good, bad):
open_document= open(document_path)
file_name = (os.path.basename(document_path))
with open ((('fqdn_') +(str(file_name).rstrip('.csv'))) , 'w') as output:
with open_document as file:
fqdn_data = csv.writer(output)
reader = csv.reader(file)
good_nums = good
bad_nums = bad
maybe_nums = []
for row in reader:
if row[3] in good_nums:
fqdn_data.writerow([row[2]])

虽然@tomgalpin的答案很好,但您也可以在scrip1.py中使用它:

import os
import csv
import re
import __main__.document_path
open_document= open(document_path)
file_name = (os.path.basename(document_path))
def start_s1(good, bad):
with open ((('fqdn_') +(str(file_name).rstrip('.csv'))) , 'w') as output:
with open_document as file:
fqdn_data = csv.writer(output)
reader = csv.reader(file)
good_nums = good
bad_nums = bad
maybe_nums = []
for row in reader:
if row[3] in good_nums:
fqdn_data.writerow([row[2]])

这在您的main.py脚本中:

import sys
import os
import csv
import pandas as pd
from maybe import start_maybe 
document_path = input("What is the file path? ")
from script1 import start_s1
open_document = open(document_path) #filelocation 
good = input("What is/are the good numbers? ")
bad = input("What is/are the bad numbers? ")
function1 = start_s1(good,bad)
print ("FQDN document created!")
maybeasn = start_maybe(good,bad)
print("Directory created for manual review, file is located inside")

script.py中的__main__模块是Python的魔力之一。

相关内容

最新更新