gpt4 book ai didi

python - python 中 main 中的其他模块无法识别 input()

转载 作者:行者123 更新时间:2023-12-01 00:19:10 25 4
gpt4 key购买 nike

我有两个脚本,其中一个是 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

非常感谢任何帮助。

最佳答案

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

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


脚本1.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]])

关于python - python 中 main 中的其他模块无法识别 input(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59074810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com