gpt4 book ai didi

带有 envoy 的 Python,如何使用变量

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:07 25 4
gpt4 key购买 nike

我尝试使用 Python envoy以便更好地访问 Shell 程序。但我有一个问题,我想删除一个文件夹的所有项目,但特使无法处理我的变量:

import envoy
import os

# Check for a empty folder.
if len(os.listdir(csv_save_path)) != 0:
for item in os.listdir(csv_save_path):
result = envoy.run('rm -v',item)
print result.std_out
print result.std_err

输出:

"rm: missing operand\nTry `rm --help' for more information.\n"

我不想使用子进程。任何人都知道出了什么问题?

编辑:

感谢您的快速回复。现在我收到此错误消息:

    Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/site-packages/envoy/core.py", line 40, in target
bufsize=0,
File "/usr/lib64/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

*** AttributeError: 'NoneType' object has no attribute 'returncode'

但是我检查了文件的路径,无论有没有路径,它都失败了。

编辑:解决方案是使用 %s 将项目放到正确的位置。

import envoy
import os

# Check for a empty folder.
if len(os.listdir(csv_save_path)) != 0:
for item in os.listdir(csv_save_path):
result = envoy.run('rm -v %s' % item)
print result.std_out
print result.std_err

到目前为止

最佳答案

envoy.run 的第二个参数是超时时间。你想要:

# The check for the empty folder is superfluous
for item in os.listdir(csv_save_path):
filepath = os.path.join(csv_save_path, item)
result = envoy.run(['rm', '-v', '--', filepath])
print (result.std_out)
print (result.std_err)

顺便问一下,为什么你使用命令行 rm 而不是 Python 的 os.unlink

关于带有 envoy 的 Python,如何使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com