例如,我在PyCharm中有:
脚本1.py
from selenium import webdriver
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.youtube.com/?hl=pl&gl=PL")
time.sleep(1)
driver.close()
脚本2.py
import YtTest.py
import time
i = 0
while i < n:
execfile("YtTest.py")
i += 1
如何正确导入并执行Script1.py并循环N次(全部在Script2.py中)
您可以在脚本1中创建如下函数:
脚本1.py
脚本2.py
以适应你的需要
通常,人们会为此使用函数
您也可以通过巧妙地使用
__name__
属性独立运行脚本脚本1.py
脚本2.py
此代码样式对于各种与导入相关的活动非常有用,例如unit testing
相关问题 更多 >
编程相关推荐