小编ima*_*hat的帖子

为什么Popen.communicate()返回b'hi \n'而不是'hi'?

有人可以解释为什么我想要的结果,"嗨",前面有一个字母'b',后跟一个换行符?

我使用的是Python 3.3

>>> import subprocess
>>> print(subprocess.Popen("echo hi", shell=True,
                           stdout=subprocess.PIPE).communicate()[0])
b'hi\n'
Run Code Online (Sandbox Code Playgroud)

如果我用python 2.7运行它,则不会出现这个额外的'b'

python subprocess popen

61
推荐指数
4
解决办法
6万
查看次数

Python Popen:同时写入stdout和日志文件

我正在使用Popen调用一个shell脚本,该脚本不断将其stdout和stderr写入日志文件.有没有办法连续同时输出日志文件(到屏幕),或者让shell脚本同时写入日志文件和标准输出?

我基本上想在Python中做这样的事情:

cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
Run Code Online (Sandbox Code Playgroud)

再次,这将stderr/stdout连接到tee,将它写入stdout和我的日志文件.

我知道如何在Python中将stdout和stderr写入日志文件.我被困在哪里是如何将这些复制回屏幕:

subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
Run Code Online (Sandbox Code Playgroud)

当然我可以做这样的事情,但有没有办法在没有tee和shell文件描述符重定向的情况下做到这一点?:

subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
Run Code Online (Sandbox Code Playgroud)

python subprocess popen

38
推荐指数
3
解决办法
4万
查看次数

Python:并行运行子进程

我有以下代码将md5sums写入日志文件

for file in files_output:
    p=subprocess.Popen(['md5sum',file],stdout=logfile)
p.wait()
Run Code Online (Sandbox Code Playgroud)
  1. 这些是并行写的吗?即如果md5sum对其中一个文件花费很长时间,那么在等待前一个文件完成之前是否会启动另一个文件?

  2. 如果上面的答案是肯定的,我可以假设写入日志文件的md5sums的顺序可能因md5sum对每个文件的使用时间而有所不同吗?(有些文件可能很大,有些文件很小)

python subprocess

21
推荐指数
3
解决办法
6万
查看次数

如何判断我的GCC默认是否正在编译64位?

有没有办法知道GCC是否默认编译32位或64位代码?

我的GCC版本是4.1.2.我的os内核版本是x86_64.

谢谢.

gcc 32bit-64bit

13
推荐指数
1
解决办法
1万
查看次数

Python:基数为10的int()的无效文字:'808.666666666667'

我有一个脚本解析csv文件中的数据.以下行给了我一些问题:

countData.append([timeStamp,int(my_csv[row][5])])
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

ValueError: invalid literal for int() with base 10: '808.666666666667' 
Run Code Online (Sandbox Code Playgroud)

csv文件的行如下:

2013-06-12 15:09:00,svcName,0,0,10,808.666666666667
Run Code Online (Sandbox Code Playgroud)

我试过int(808.666666666667)在python提示符下运行,它工作正常.

python

13
推荐指数
1
解决办法
2万
查看次数

maven:`-U, - update-snapshots`真的有用吗?

在命令行帮助中,我看到maven"检查"更新:

-U,--update-snapshots                  Forces a check for updated
                                       releases and snapshots on remote
                                       repositories
Run Code Online (Sandbox Code Playgroud)

但是,Stack Overflow上的大多数问题都意味着此选项会强制Maven更新.这是否意味着它强制重新下载依赖项?

例如/sf/answers/678857931/

maven

11
推荐指数
2
解决办法
1万
查看次数

为什么不捕获=“用户”将手机的摄像头更改为正面?

我尝试了以下属性capture来设置input元素以尝试选择前置摄像头。user是正面,environment背面。

但是,在移动设备(Pixel 1,最新的操作系统)上试用Chrome(最新)时,仍然选择了后置摄像头。

我使用了一个示例片段:

<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="image" accept="image/*" capture="user">
  <input type="submit" value="Upload">
</form>
Run Code Online (Sandbox Code Playgroud)

(来自https://w3c.github.io/html-media-capture/#the-capture-attribute

我可以更改手机accept上的要求video,以便该属性和其他属性起作用,但该capture属性不能起作用。

编辑:正在使用最新的Android Chrome浏览器。下一页暗示iOS和旧版Android Chrome不能一起使用capture,但可以使用capturehttps://caniuse.com/#search=html-media-capture

html android mobile-chrome mediacapture

11
推荐指数
1
解决办法
262
查看次数

Vagrant一​​直试图使用AWS插件.怎么停?

我只是vagrant up第一次尝试使用标准的Ubuntu映像.我的公司已经在我的笔记本电脑上设置了Vagrant并为AWS安装了一些插件.当我尝试vagrant up使用单独的Vagrantfile 运行我的个人Ubuntu映像时,出现以下错误:

There are errors in the configuration of this machine. Please fix
the following errors and try again:

AWS Provider:
* An access key ID must be specified via "access_key_id"
* A secret access key is required via "secret_access_key"
* An AMI must be configured via "ami" (region: #{region})
Run Code Online (Sandbox Code Playgroud)

我不是想连接到AWS.我只想在笔记本电脑上设置我的第一张个人照片.

vagrant

6
推荐指数
1
解决办法
709
查看次数

Argparse:如何接受具有不同"类型"的参数

我想接受以下样式的参数:

python3 test.py -r SERVICE=100,101,102 -r SERVICE2=1,2,3,4,5 
(-r REPO=REV#,REV#,etc... -r etc...)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已完成以下操作(添加了参数-r并定义了类型转速).它应该传回两个列表,这可能会有问题.

import argparse

def revs(s):
    try:            
        REPO, REVISIONS = map(str, s.split('='))
        REVISIONS = map(int, REVISIONS.split(','))
        return REPO, REVISIONS
    except:
        raise argparse.ArgumentTypeError("Must be format -r REPO=REV,REV,etc.. e.g. SERVICES=181449,181447") 

parser.add_argument('-r', type='revs', nargs='*', action='append', help='Revisions to update. The list is prefixed with the name of the source repository and a "=" sign. This parameter can be used multiple times.', required=True)
Run Code Online (Sandbox Code Playgroud)

运行上述内容时出现以下错误:

Traceback (most recent call last):
File "test.py", line 11, in <module> …
Run Code Online (Sandbox Code Playgroud)

python argparse python-3.3

5
推荐指数
1
解决办法
3886
查看次数

在Django-rest-framework HyperlinkedModelSerializer上解析域名

我希望DRF用于序列化的超链接:

http://<mydomain.com>/api/v1/endpoint
Run Code Online (Sandbox Code Playgroud)

而不是

http://127.0.0.1/api/v1/endpoint
Run Code Online (Sandbox Code Playgroud)

可以在Django中配置它还是与我的http服务器配置(gunicorn + nginx)相关?

django django-rest-framework

5
推荐指数
1
解决办法
751
查看次数