我是python的新手,我想知道这行指令出了什么问题。在Python文档中的subprocess示例中使用了这行代码:
subprocess.call(["ls", "-l"])
它基本上返回了:
WindowsError: [Error 2] The system cannot find the file specified
Python认为"ls“是一个文件而不是一个参数。有没有办法解决这个问题?
我不明白我陷入了什么样的困境,请有人解释一下这里发生了什么,我该如何解决呢?命令行截图
$ python --version
Python 3.6.5
$ which python
/usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7
编辑:命令type -a python,屏幕截图的输出
$ type -a python
python is aliased to 'python3.6'
python is /
它似乎在两个地方中断,一个是">",另一个是"/tmp/testing“。
Python 2.7.5 (default, Aug 29 2016, 10:12:21)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subp
我创建了以下运行在Linuxpythonver2.x上的简单python脚本
#!/usr/bin/python
def GetListfiles():
LS = (commands.getstatusoutput(" ls "))
return LS
AA = GetListfiles()
for filename in AA:
print "------------"
print filename
在运行我的脚本之后,我得到了以下输出:
file.txt
file1.txt
f
我可以在linux中使用以下命令。
ls > >(tee -a a)
但是python os.system不支持这种语法。
>>> import os
>>> os.system("ls > >(tee -a a)") sh: -c: line 0: syntax error near unexpected token `>' sh: -c: line 0: `ls > >(tee -a)' 256
>>>
我们需要任何特殊的方法来转义任何字符吗?
我想使用call子进程运行这个命令。
ls -l folder | wc -l
我在Python文件中的代码如下:
subprocess.call(["ls","-l","folder","|","wc","-l"])
我收到了这样一条错误信息:
ls: cannot access |: No such file or directory
ls: cannot access wc: No such file or directory
这就像命令|wc不能被调用子进程读取一样。
我怎么才能修好它?
我尝试过各种方法将stdout和stderr重定向到/dev/null,但没有成功。我几乎有我的整个生命运行bash,我从来没有遇到过这个问题,但第一次在BSD,我被困在/bin/sh。
我试过的是:
if ls ./python* 2> /dev/null; then
echo found Python
fi
..。如果Python不存在,它将静音来自ls的错误消息。但是,如果存在python.tgz,则输出如下所示的一行:
# ./test.sh
./python-2.7.3p1.tgz
我试过:
if ls ./python* &> /dev/null; th
这是巴什。这种行为在鱼身上是相似的。
$ which python
/usr/bin/python
$ alias py=python
$ type py
py is aliased to `python'
然后,运行type -P py将不会输出任何内容,正如我所期望的那样,它将以类似于下面所示的方式打印/usr/bin/pyton。
$ type ls
ls is aliased to `ls --color=auto'
$ type -P ls
/bin/ls
-P选项的文档读取
-P force a PATH search for each NAME,
只是好奇为什么会给出这个输出,对于其他魔术命令,追加?会给出一个文档字符串,例如?%pwd shows:
Return the current working directory path.
Examples
--------
::
In [9]: pwd
Out[9]: '/home/nuser/sprint/ipython'
File: c:\program files\python38\lib\site-packages\ipython\core\magics\osm.py
为什么在中运行?%ls会显示Repr: <alias ls for &
我有一个很长的文件列表要处理,在python编程的10个步骤中,我试图创建一个makefile来运行所有这些文件。出于某种原因,我不知道为什么,当我像下面的代码一样运行它们时,第二步之后输出为空。当我逐个调用它们时,代码可以工作,但我在每个步骤中更改了输出的名称,或者更改了文件夹中的输出,我不知道如何在makefiles中实现它。我也接受除了makefiles之外的其他建议(比如pypelines ...我对shell命令有点迷惑)
我的makefile看起来像这样
zero::
for f in `ls data/wikiMaths/*.html`; do \
我不知道我做了什么,但是像"ls“和"sudo”这样的基本命令不再起作用了,现在告诉我这个错误信息。
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFou
我有当当前目录变得无效的情况(例如,当某个程序删除它时)。我的Python脚本调用os.getcwd(),它以以下异常终止
OSError: [Errno 2] No such file or directory
理想情况下,在这种情况下,我的脚本会自动进入父目录。推荐的实施策略是什么时候?
注意,如果我从外壳程序中尝试ls,我会得到ls: cannot open directory .: Stale NFS file handle
我正在尝试创建自己的自定义Sagemaker,该框架运行自定义python脚本,使用entry_point参数来训练ML模型。
在Python文档()之后,我编写了最简单的代码来运行培训作业,仅仅是为了了解它的行为以及它是如何工作的。
我的问题是,我不知道如何正确构建我的Docker容器,以便运行entry_point脚本。
我将train.py脚本添加到容器中,该容器只记录文件夹和文件路径以及容器环境中的变量。
我能够运行培训任务,但在环境变量和容器中的文件中都找不到对entry_point脚本的任何引用。
下面是我使用的代码:
Custom Sagemaker框架Class:
from s
如果我有一个具有string字段的python类的列表,那么是否有方法使用in将字符串与该类的对象列表进行比较?我很好奇是否有办法这样做:
class Foo:
def __init__(self, s):
self.s = s
bar = Foo('test')
ls = [bar]
if 'test' in ls:
print("Yay!")
通过修改__eq__方法,甚至可能有一个我不知道的__in__方法
我正在创建一个程序,它将输入一个帐号列表,然后运行一个ls -lh命令来为每个帐户找到一个文件。当我在没有Python的Linux服务器上运行我的命令时,它没有问题,但是当我通过Python执行命令时,它说它找不到它们。
import subprocess as sp
sp.call(['cd', input_dir])
for i, e in enumerate(piv_id_list):
proc_out = sp.Popen(['ls', '-lh', '*CSV*APP*{0}.zip'.format(e)])
我试图摆脱我的python代码中的os.system,大部分都已经完成了,但我有一行代码似乎无法转换。
os.system('/bin/sh -i -c "/bin/runner catch exts"')
到目前为止,尝试过的东西如下:
p = subprocess.Popen(['/bin/sh', '-i', '-c', '/bin/runner catch exts'])
这将返回:
/bin/sh: 1: Cannot set tty process group (No such proc
我需要处理一个字符串数组。由于字符串可以独立处理,所以我将并行处理:
import multiprocessing
import numpy as np
def func(x):
ls = ["this", "is"]
return [i.upper() for i in x.split(' ') if i not in ls]
arr = np.asarray(["this is a test", "this is not a test", "see my good example
不久前,我做了一个Python脚本,看起来像这样:
with open("somefile.txt", "r") as f, open("otherfile.txt", "a") as w:
for line in f:
w.write(line)
当然,它在100mb文件上的运行速度非常慢。
但是,我更改了程序来实现这一点
ls = []
with open("somefile.txt", "r") as f, open("otherfile.txt", &
我在我的bash脚本中有这个
for FOLDERNAME in `\ls -1 /home/`
do
if [ ! -d /backups/home/${date} ]; then
mkdir /backups/home/${date}
chmod 777 /backups/home/${date}
/bin/chgrp ${FOLDERNAME} /backups/home/${FOLDERNAME}
usermod -a -G ${FOLDERNAME} john
fi
如何将其转换为python代码?
我想使用子进程模块在pythong中运行一个命令,并将输出存储在一个变量中。但是,我不希望将命令的输出打印到终端。对于此代码:
def storels():
a = subprocess.Popen("ls",shell=True)
storels()
我在终端中获得目录列表,而不是将其存储在a中。我也尝试过:
def storels():
subprocess.Popen("ls > tmp",shell=True)
a = open("./tmp")
[Rest of Code]
st