如何在两个不同的python脚本之间传递变量
我试图在两个不同的python脚本之间传递信息。它们相当长,因此为了简化起见,这里有两个我遇到相同问题的其他脚本: a、 派克 b、 派克 我被要求输入num和tipe两次,然后第二次的条目被打印11次 如何使a.py中的变量/文件传递到b.py,在b.py中编辑/操作/操作,然后在a.py中传递回/关闭如何在两个不同的python脚本之间传递变量,python,variables,import,module,Python,Variables,Import,Module,我试图在两个不同的python脚本之间传递信息。它们相当长,因此为了简化起见,这里有两个我遇到相同问题的其他脚本: a、 派克 b、 派克 我被要求输入num和tipe两次,然后第二次的条目被打印11次 如何使a.py中的变量/文件传递到b.py,在b.py中编辑/操作/操作,然后在a.py中传递回/关闭 另外,为什么我要两次输入num和tipe,然后ifname='main':下的代码运行?在Python脚本之间传递变量时,请记住,调用脚本时,调用脚本可以访问被调用脚本的命名空间 也就是说,您
另外,为什么我要两次输入num和tipe,然后ifname='main':下的代码运行?在Python脚本之间传递变量时,请记住,调用脚本时,调用脚本可以访问被调用脚本的命名空间 也就是说,您可以尝试一下:用代码启动被调用的脚本
from __main__ import *
这将授予对调用方脚本的命名空间(变量和函数)的访问权。由于这些文件实际上并不是您之前所说的您将要处理的文件,我将把它留给您来应用于真正的文件,希望这能有所帮助 在Python脚本之间传递变量时,请记住,调用脚本时,调用脚本可以访问被调用脚本的命名空间 也就是说,您可以尝试一下:用代码启动被调用的脚本
from __main__ import *
这将授予对调用方脚本的命名空间(变量和函数)的访问权。由于这些文件实际上并不是您之前所说的您将要处理的文件,我将把它留给您来应用于真正的文件,希望这能有所帮助 您可以通过函数传递它们 a.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
b.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
执行a.py将导致
3 fruits
3 fruits
4 fruits
5 fruits
6 fruits
7 fruits
8 fruits
9 fruits
10 fruits
11 fruits
12 fruits
3水果打印两次,因为您首先调用函数,然后增加num(通过重新分配)。相反,您可以将a.py如下所示,只打印一次3种水果:
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(x, tipe, f) # Pass parameters num, tipe and file handle
f.close()
您可以通过函数传递它们 a.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
b.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
执行a.py将导致
3 fruits
3 fruits
4 fruits
5 fruits
6 fruits
7 fruits
8 fruits
9 fruits
10 fruits
11 fruits
12 fruits
3水果打印两次,因为您首先调用函数,然后增加num(通过重新分配)。相反,您可以将a.py如下所示,只打印一次3种水果:
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(x, tipe, f) # Pass parameters num, tipe and file handle
f.close()
你可以用这种方式
a.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
b.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
你可以用这种方式
a.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
b.py
f = open('test.txt', 'w+')
num = int(raw_input('How many are there: '))
tipe = raw_input('What kind are they: ')
if __name__ == '__main__':
from b import fxn
for x in xrange(num, num+11):
fxn(num, tipe, f) # Pass parameters num, tipe and file handle
num = x
f.close()
# from a import num, tipe --> **This is not required**
# receive inputs
def fxn(num, tipe, f):
print num, tipe
f.writelines(str(num)+', '+tipe)
x=5
print x
import a
print a.x
函数可以接受参数。要求您输入两次的原因是这些IO操作在全局范围内,每次导入a时都会执行。函数可以获取参数。要求您两次输入的原因是,这些IO操作在全局范围内,每次导入时都会执行。