re.split() 在解释器中工作,但在脚本执行时失败。 Python 3
re.split() works in the interpreter, but, fails on script execution. Python 3
我遇到了一个奇怪的问题。我正在尝试编写一个脚本来自动执行 NFS 挂载,但它似乎在 re.split 上失败了。我想使用任意数量的空格来分隔字符串,但是,由于某种原因,当我 运行 脚本失败时。当我 运行 我的脚本时,我生成了以下错误。
basilius@HomeComing:~/PycharmProjects/pythonProject1$ sudo python3 mount_py3.py lin
file.txt rw,noac,suid
Enter the name of the default group: basilius
Enter the default group name: basilius
Traceback (most recent call last):
File "mount_py3.py", line 146, in <module>
main()
File "mount_py3.py", line 125, in main
export, mount_point = re.split(' +', line)
ValueError: not enough values to unpack (expected 2, got 1)
对于以下代码。
inp_file = open(args.filein, 'r')
for line in inp_file.readline():
export, mount_point = re.split(' +', line)
我使用 argparse 将脚本名称作为字符串传递给脚本。它没有被 argparse 打开。
当我直接调用解释器时它工作正常。见下文。
basilius@HomeComing:~/PycharmProjects/pythonProject1$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> inp_file = open('file.txt', 'r')
>>> line = inp_file.readline()
>>> print(line)
server:/opt/website /srv/program
>>> export, mount_point = re.split(' +', line)
>>> print(export, mount_point)
server:/opt/website /srv/program
>>>
当我在文件上直接执行 readlines() 时,它 returns 一切都以正确的格式显示。
它是用于导出的纯文本文件,mount_point用于 fstab 条目。我不确定为什么会得到不同的结果。有人可以帮忙吗?这几天我一直在上网。
问题出在你的循环上,你写 for line in inp_file.readline():
的地方。这将从文件中读取一行,并遍历字符串中的字符,依次将每个字符分配给 line
。
您可能需要 for line in inp_file:
,它一次一个循环遍历文件中的行。您还可以在文件上调用 readlines()
(末尾有一个“s”),它做同样的事情但使用更多内存。
或者,我想,如果您只关心文件的第一行,您可以只执行 line = inp_file.readline()
而无需 for
循环。
与您的问题无关,使用 with
语句来处理文件的打开和关闭可能是个好主意:with open(args.filein, 'r') as inp_file:
,然后是使用它的其余代码缩进一级。
我遇到了一个奇怪的问题。我正在尝试编写一个脚本来自动执行 NFS 挂载,但它似乎在 re.split 上失败了。我想使用任意数量的空格来分隔字符串,但是,由于某种原因,当我 运行 脚本失败时。当我 运行 我的脚本时,我生成了以下错误。
basilius@HomeComing:~/PycharmProjects/pythonProject1$ sudo python3 mount_py3.py lin
file.txt rw,noac,suid
Enter the name of the default group: basilius
Enter the default group name: basilius
Traceback (most recent call last):
File "mount_py3.py", line 146, in <module>
main()
File "mount_py3.py", line 125, in main
export, mount_point = re.split(' +', line)
ValueError: not enough values to unpack (expected 2, got 1)
对于以下代码。
inp_file = open(args.filein, 'r')
for line in inp_file.readline():
export, mount_point = re.split(' +', line)
我使用 argparse 将脚本名称作为字符串传递给脚本。它没有被 argparse 打开。
当我直接调用解释器时它工作正常。见下文。
basilius@HomeComing:~/PycharmProjects/pythonProject1$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> inp_file = open('file.txt', 'r')
>>> line = inp_file.readline()
>>> print(line)
server:/opt/website /srv/program
>>> export, mount_point = re.split(' +', line)
>>> print(export, mount_point)
server:/opt/website /srv/program
>>>
当我在文件上直接执行 readlines() 时,它 returns 一切都以正确的格式显示。
它是用于导出的纯文本文件,mount_point用于 fstab 条目。我不确定为什么会得到不同的结果。有人可以帮忙吗?这几天我一直在上网。
问题出在你的循环上,你写 for line in inp_file.readline():
的地方。这将从文件中读取一行,并遍历字符串中的字符,依次将每个字符分配给 line
。
您可能需要 for line in inp_file:
,它一次一个循环遍历文件中的行。您还可以在文件上调用 readlines()
(末尾有一个“s”),它做同样的事情但使用更多内存。
或者,我想,如果您只关心文件的第一行,您可以只执行 line = inp_file.readline()
而无需 for
循环。
与您的问题无关,使用 with
语句来处理文件的打开和关闭可能是个好主意:with open(args.filein, 'r') as inp_file:
,然后是使用它的其余代码缩进一级。