如何循环.py文件python导入模块?
我有一个程序,要求用户意图。他们可以决定上传,检索,重置或设置首选项。问题是当他们上传信息时,我需要两个py文件才能在循环中运行,直到满足需求为止。我看起来像这样的东西:
def MainTerminal():
import User_Intent
import ProceduresTerminal
determine_intent = open(gvar.procedure_upload, 'r')
if determine_intent.read() == '1':
determine_intent.close()
import Upload_Data
import Data_Processing
R_Check = open(gvar.RActivation, 'r')
R_Check_Read = R_Check.read()
R_Check.close()
if R_Check_Read == '1':
import C_Processing
while R_Check_Read == '1':
import R_Processing
import C_Processing
else:
import C_Processing
如果函数起作用,则while循环无法正常工作。有其他方法可以做到吗?在while循环中,C_Processing应该在将R_CHECK_READ设置为'0'时结束循环,此部分正在工作,但while loop却行不通。
I have a program that asks the user for their intent. They can decide to either Upload, Retrieve, Reset, or Set Preferences. The problem is when they upload information I need two py files to run in a loop until the requirement is met. What I have looks like sorta like this:
def MainTerminal():
import User_Intent
import ProceduresTerminal
determine_intent = open(gvar.procedure_upload, 'r')
if determine_intent.read() == '1':
determine_intent.close()
import Upload_Data
import Data_Processing
R_Check = open(gvar.RActivation, 'r')
R_Check_Read = R_Check.read()
R_Check.close()
if R_Check_Read == '1':
import C_Processing
while R_Check_Read == '1':
import R_Processing
import C_Processing
else:
import C_Processing
The if functions work however, the while loop does not function. Is there a different way to do this? In the While loop C_Processing is supposed to end the loop when it sets R_Check_Read to '0', this part is working but the while loop does not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。
将所有
导入
语句放在文件顶部。他们应该
def
一些功能。 循环中的中调用一对此类功能。
导入模块应大部分仅定义函数,
再加上一些变量。
避免
print()
语句或进口时间发生的其他副作用。将这些操作推迟到运行时间,当调用者调用您的功能时。
了解导入结果被缓存,
因此,重复导入同一模块将无效。
使用 isort 管理您的导入。
(他们的座右铭?“我对您的进口进行排序,所以您不必这样做。”)
Yes.
Put all your
import
statements at top-of-file.They should
def
some functions. Call a pair of such functions within yourwhile
loop.Importing a module should mostly just define functions,
plus maybe a few variables.
Avoid
print()
statements or other side effects that happen at import time.Defer such actions until run time, when caller invokes your function.
Understand that import results are cached,
so repeated import of same module will have no effect.
Use isort to manage your imports.
(Their motto? "I sort your imports, so you don't have to.")