input() 未被 python 中 main 中的其他模块识别
input() not being recognized by other module from main in python
我有两个脚本,一个是 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
非常感谢任何帮助。
根据我的评论,您应该重组代码并在 modules/functions.
之间传递 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的魔法之一。
我有两个脚本,一个是 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
非常感谢任何帮助。
根据我的评论,您应该重组代码并在 modules/functions.
之间传递 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的魔法之一。