python-2.7

Python: How to run multiple commands in one process using popen

偶尔善良 提交于 2021-02-19 01:20:51
问题 I want to open a process and run two commands in the same process. I have : cmd1 = 'source /usr/local/../..' cmd2 = 'ls -l' final = Popen(cmd2, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, nothing = final.communicate() log = open('log', 'w') log.write(stdout) log.close() If I use popen two times, these two commands will be executed in different processes. But I want them to run in the same shell. 回答1: The commands will always be two (unix) processes, but you can

Python: How to run multiple commands in one process using popen

孤者浪人 提交于 2021-02-19 01:20:29
问题 I want to open a process and run two commands in the same process. I have : cmd1 = 'source /usr/local/../..' cmd2 = 'ls -l' final = Popen(cmd2, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, nothing = final.communicate() log = open('log', 'w') log.write(stdout) log.close() If I use popen two times, these two commands will be executed in different processes. But I want them to run in the same shell. 回答1: The commands will always be two (unix) processes, but you can

I am getting this error “TypeError: str() takes at most 1 argument (2 given)” at “client_response” variable

蹲街弑〆低调 提交于 2021-02-18 22:55:43
问题 EDIT to format: This is the original code from __future__ import print_function import socket import sys def socket_accept(): conn, address = s.accept() print("Connection has been established | " + "IP " + address[0] + "| Port " + str(address[1])) send_commands(conn) conn.close() def send_commands(conn): while True: cmd = raw_input() if cmd == 'quit': conn.close() s.close() sys.exit() if len(str.encode(cmd)) > 0: conn.send(str.encode(cmd)) client_response = str(conn.recv(1024), "utf-8") print

I am getting this error “TypeError: str() takes at most 1 argument (2 given)” at “client_response” variable

旧街凉风 提交于 2021-02-18 22:55:08
问题 EDIT to format: This is the original code from __future__ import print_function import socket import sys def socket_accept(): conn, address = s.accept() print("Connection has been established | " + "IP " + address[0] + "| Port " + str(address[1])) send_commands(conn) conn.close() def send_commands(conn): while True: cmd = raw_input() if cmd == 'quit': conn.close() s.close() sys.exit() if len(str.encode(cmd)) > 0: conn.send(str.encode(cmd)) client_response = str(conn.recv(1024), "utf-8") print

Obtaining length of list as a value in dictionary in Python 2.7

倖福魔咒の 提交于 2021-02-18 22:28:09
问题 I have two lists and dictionary as follows: >>> var1=[1,2,3,4] >>> var2=[5,6,7] >>> dict={1:var1,2:var2} I want to find the size of the mutable element from my dictionary i.e. the length of the value for a key. After looking up the help('dict') , I could only find the function to return number of keys i.e. dict.__len__() . I tried the Java method(hoping that it could work) i.e. len(dict.items()[0]) but it evaluated to 2 . I intend to find this: Length of value for first key: 4 Length of value

Forward slash in json file from pandas dataframe

為{幸葍}努か 提交于 2021-02-18 22:11:26
问题 I'm a complete newbie to json, any help is appreciated. I'm trying to convert a dataframe to a json file. import pandas as pd df = pd.DataFrame({ 'A' : [1., 2.5], 'B' : ['img/blue.png', 'img/red.png']}) print df Output is A B 0 1.0 img/blue.png 1 2.5 img/red.png I would like to make a json file that looks like this: '[1.0,"img/blue.png"],[2.5,"img/red.png"]' However, when I use the following out = df.to_json(orient='values')[1:-1] print out I get this instead '[1.0,"img\\/blue.png"],[2.5,"img

Turn off logging in schedule library

荒凉一梦 提交于 2021-02-18 20:48:06
问题 Objective: prevent schedule from logging every time it runs. Background: I am using the logging and schedule libraries in a python project. My log file contains information about the physical state of a instrument run by a Raspberry Pi, and is updated every 10 seconds. I use the schedule library to schedule that periodic log. Here is the limited documentation I have found for schedule . The Problem: The schedule library logs this statement, every time it runs a job. 2016-06-29 09:01:51,022

run python-ldap with official python docker image as base

只谈情不闲聊 提交于 2021-02-18 20:30:56
问题 I am using the official python docker image for python 2.7. The application I am working with requires pyhon-ldap. My dockerfile looked like this: FROM python:2.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ Where python-ldap is in requirements.txt Naturally I run into this: In file included from Modules/LDAPObject.c:9:0: Modules/errors.h:8:18: fatal error: lber.h: No such file or directory #include "lber.h"

run python-ldap with official python docker image as base

假装没事ソ 提交于 2021-02-18 20:30:07
问题 I am using the official python docker image for python 2.7. The application I am working with requires pyhon-ldap. My dockerfile looked like this: FROM python:2.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ Where python-ldap is in requirements.txt Naturally I run into this: In file included from Modules/LDAPObject.c:9:0: Modules/errors.h:8:18: fatal error: lber.h: No such file or directory #include "lber.h"

LINK : fatal error LNK1104: cannot open file 'python27.lib'

坚强是说给别人听的谎言 提交于 2021-02-18 20:13:04
问题 I was trying to compile pycrypto-2.6.1 from source for Python 2.7.10 64-Bit Windows Version and facing the following error. Processing pycrypto-2.6.1.tar.gz Writing c:\users\sivasuba\appdata\local\temp\easy_install-ecznz_\pycrypto-2.6.1\setup.cfg Running pycrypto-2.6.1\setup.py -q bdist_egg --dist-dir c:\users\sivasuba\appdata\local\temp\easy_install-ecznz_\pycrypto-2.6.1\egg-dist-tmp-us3gka warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. winrand.c LINK :