Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

python - Python:将数据从一个文件传递到另一个文件(Python: Pass Data From One File to Another)

For example, I have two files, a.py, and b.py.

(例如,我有两个文件a.py和b.py。)

I would like b.py to access data from a.py.

(我希望b.py从a.py访问数据。)

#a.py

import random
import subprocess


def rnd():
    return random.random()


random_number_1 = rnd()


if random_number_1 > 0.5:
    subprocess.Popen(['C:/Users/ex/AppData/Local/Programs/Python/Python38/python.exe', 'b.py'])

-

(--)

#b.py
#get the number generated from a.py and print it.

What I would like: Each time a.py is run, a new number is generated.

(我想要的是:每次运行a.py时,都会生成一个新数字。)

But, each time b.py is run, the same number will be printed, until a.py is run again.

(但是,每次运行b.py时,都会打印相同的数字,直到再次运行a.py为止。)

(Python 3.8)

((Python 3.8))

EDIT: I have seen some similar questions for older versions of Python resolved with pickle and mmap.

(编辑:我已经看到一些类似的问题,用pickle和mmap解决了旧版本的Python。)

But I am not familiar with those modules.

(但是我对那些模块不熟悉。)

Could they be helpful?

(他们会有所帮助吗?)

I wasn't able to get the examples to work.

(我无法使示例工作。)

  ask by Xa1 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use sys.argv for that:

(您可以为此使用sys.argv :)

#a.py

import random
import subprocess


def rnd():
    return random.random()


random_number_1 = rnd()


if random_number_1 > 0.5:
    subprocess.Popen(['C:/Users/ex/AppData/Local/Programs/Python/Python38/python.exe', 'b.py', str(random_number_1)])

And in b.py you can access that number:

(在b.py中,您可以访问该号码:)

#b.py
import sys
random_number_1 = float(sys.argv[1])
print(sys.argv[1])

So what is sys.argv ?

(那么sys.argv是什么?)

For example when you write that in command prompt:

(例如,当您在命令提示符下编写该代码时:)

C:> pip install my_module

In pip program it access to "install" and "my_module" like that:

(在pip程序中,可以像这样访问“安装”和“ my_module”:)

import sys
sys.argv[0] # full path of program (for example C:UsersUserAppDataLocalProgramsPythonPython37Scriptspip.exe)
sys.argv[1] # "install"
sys.argv[2] # "my_module"

In this way you can only pass that number for one time, it will not change in b.py if you change it in a.py.

(这样,您只能将该数字传递一次,如果在a.py中进行更改,则该数字在b.py中将不会更改。)

Also if you want you can write that number to a file:

(另外,如果您愿意,可以将该数字写入文件中:)

#a.py

import random
import subprocess
import pickle

def rnd():
    return random.random()


random_number_1 = rnd()

with open("C:\data.txt","wb") as f:
    f.write(pickle.dumps(random_number_1))


if random_number_1 > 0.5:
    subprocess.Popen(['C:/Users/ex/AppData/Local/Programs/Python/Python38/python.exe', 'b.py'])

And in b.py you can read that file:

(在b.py中,您可以读取该文件:)

#b.py
import pickle
with open("C:\data.txt","rb") as f:
    random_number_1 = pickle.load(f)
print(random_number_1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...