在 Python 中调用 .x 文件 (Linux) 并列出
我有以下代码:
# Initialisations
filename='/home/Admin/Desktop/Ncapa/i_capa.txt'
f = open(filename, 'r')
ty = "r"
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
d = float(f.readline())
D = float(f.readline())
BB = float(f.readline())
vrR = float(f.readline())
P = float(f.readline())
f0 = float(f.readline())
f1 = float(f.readline())
f.close()
os.system('ls -lt > ./capacite.x')
filename2 = '/home/Admin/Desktop/Ncapa/o_capa.txt'
f = open(filename2, 'r')
f.readline()
f.readline()
f.readline()
f.readline()
c0 = float(f.readline())
f.close()
我的第一个问题(正如您可能已经猜到的那样,是一个非常简单的问题),有没有办法将 f.readline 块简化为简单的东西?我总是对如何读取文本文件然后从列表中获取值分配给变量感到困惑。所以我最终这样做了,因为我喜欢它的工作方式,但不喜欢整个代码的外观。我没有使用 f.readlines() 因为我很困惑如何从使用它的列表中获取某些值。
我的第二个问题对应于以下行:
os.system('ls -lt > ./capacite.x')
有没有办法在 Linux 中的 Python 中执行 .x 文件?该行似乎不会引起任何问题,但我知道它也不起作用,因为该文件生成 o_capa.txt 文件,到目前为止,我必须手动填写该文件来测试程序的其余部分。
任何帮助将不胜感激=)
I have the following code:
# Initialisations
filename='/home/Admin/Desktop/Ncapa/i_capa.txt'
f = open(filename, 'r')
ty = "r"
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
d = float(f.readline())
D = float(f.readline())
BB = float(f.readline())
vrR = float(f.readline())
P = float(f.readline())
f0 = float(f.readline())
f1 = float(f.readline())
f.close()
os.system('ls -lt > ./capacite.x')
filename2 = '/home/Admin/Desktop/Ncapa/o_capa.txt'
f = open(filename2, 'r')
f.readline()
f.readline()
f.readline()
f.readline()
c0 = float(f.readline())
f.close()
My first question (as you may have already guessed, is a very simple one), is there a way of reducing the f.readline block to something simple? I always get confused how to read a text file and then take values from a list to assign to variables. So I ended up doing it this way as I like the way it works but don't like the look of the code as a whole. I haven't used f.readlines() as I get confused how to get certain values from the list you get using it.
My second question corresponding to the line:
os.system('ls -lt > ./capacite.x')
Is there a way of executing an .x file in Python in Linux? This line doesn't appear to cause any problems but I know it doesn't work either as this file produces the o_capa.txt file which up until this point I have had to fill in by hand to test the rest of my programme.
Any help would be greatly appreciated =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在类似于下面的循环中运行 readline
You can run the readline in a loop something similar to the below
如果您知道每个变量的名称和行,请创建一个字典:
然后像变量一样使用
data
字典,例如data['d']
。If you know the name and line for each variable, create a dictionary:
Then use the
data
dictionary as if it were variables, such asdata['d']
.你为什么不使用
why don't you use