由于os.popen被subprocess.popen取代,我想知道如何转换
os.popen('swfdump /tmp/filename.swf/ -d')
Run Code Online (Sandbox Code Playgroud)
到subprocess.popen()
我试过了:
subprocess.Popen("swfdump /tmp/filename.swf -d")
subprocess.Popen("swfdump %s -d" % (filename)) # NOTE: filename is a variable
# containing /tmp/filename.swf
Run Code Online (Sandbox Code Playgroud)
但我想我没有正确地写出来.任何帮助,将不胜感激.谢谢
我试图将我的变量传递raw_input给我的子进程命令.我是Python的新手.任何帮助他都会赞赏.
#!/usr/bin/python
import subprocess
print "\nWhat user name"
username = str(raw_input('username: '))
print "\nWhat is the user id"
userid = int(raw_input('Enter user id: '))
print "\nWhat is the user\'s primary group?"
primarygroup = int(raw_input('Enter group: '))
print "\nWhat is the user\'s secondary group?"
secondarygroup = int(raw_input('Enter group: '))
subprocess.call(['useradd' '-m' '-g' _primarygroup '-G' _secondarygroup '-u' _userid _username])
print"\nThe user has been added"
Run Code Online (Sandbox Code Playgroud) 1 import subprocess
2 raw = raw_input("Filename:").lower()
3 ip = raw_input("Host:").lower()
4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + " raw " + " ip ",shell=True)
Run Code Online (Sandbox Code Playgroud)
所以这是我的剧本.除了一个关键目标,我使用原始输入一切正常.它允许我输入我想要的任何东西,但是当它用于保存文件或使用ip/host时,doe实际上并没有做任何事情.当然它给了我数据包,但是从localhost而不是我输入的主机.
我怎么知道这不起作用是因为我的第一个原始输入是文件名,所以我放入测试,当我在文件夹中查看我的脚本是,它产生一个名为"raw"的文件意思,它实际上并没有带我的输入只使用我的"X"内的什么...
所以我有机会来到这个:
1 import subprocess
2 raw = raw_input("Filename:").lower()
3 ip = raw_input("Host:").lower()
4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw + "host" + ip,shell=True)
Run Code Online (Sandbox Code Playgroud)
这很好,因为它实际上需要-w但它现在将其保存为rawhostip而不是"raw"输入.作为参考,这是该命令在终端中的样子:
tcpdump -c5 -vvv -w savename host wiki2
Run Code Online (Sandbox Code Playgroud)
只有两个变量是savename和wiki2,其余的变量需要命令才能工作.
使用此脚本我收到此错误:
import subprocess
raw = raw_input("Filename:").lower()
ip = raw_input("Host:").lower()
cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" …Run Code Online (Sandbox Code Playgroud) 我正在用python创建小的控制台脚本,我想在其中放置cowsay命令,但是cow说的是变量的名称,字符串所在的位置,而不是变量内的字符串。我怎样才能让牛在变量中说出字符串?
if (command == 'cow'):
word = raw_input('What does the cow say? ')
os.system('cowsay word')
Run Code Online (Sandbox Code Playgroud) 我的书说:
在您的计算机上运行的每个程序都有一个当前工作目录或 cwd。任何不以根文件夹开头的文件名或路径都假定在当前工作目录下
因为我在 OSX 上,所以我的根文件夹是 /。当我在os.getcwd()Python shell 中输入时,我得到/Users/apple/Documents. 为什么我的 cwd 中出现 Documents 文件夹?是说 Python 正在使用 Documents 文件夹吗?没有任何以/( 根文件夹 )开头的指向 Python 的路径吗?另外,每个程序都有不同的 cwd 吗?
我正在尝试从文件中删除文件的前n行,我计算n并将该值赋给myvariable
command = "tail -n +"myvariable" test.txt >> testmod.txt"
call(command)
Run Code Online (Sandbox Code Playgroud)
我从子进程导入了调用.但我在myvariable上遇到语法错误.
我有一个基本的批处理文件,它接受用户输入:
@echo off
set /p Thing= Type Something:
echo %Thing%
pause
Run Code Online (Sandbox Code Playgroud)
但是,我想使用Python编写的变量传递到批处理文件中.让我们说一个字符串'arg1'这只是一个基本的例子,但我仍然无法弄明白.以下代码将运行批处理,但'arg1"没有影响
import subprocess
filepath = r'C:\Users\MattR\Desktop\testing.bat'
subprocess.call([filepath, 'arg1'])
Run Code Online (Sandbox Code Playgroud)
我也试过,p = subprocess.Popen([filepath, 'arg1'])但批处理文件不能在Python中运行.
我搜索过网络,但是没有一个答案似乎对我有用.以下是我也尝试过的一些链接:示例1,示例2.我也尝试过其他人,但他们似乎对用户的需求非常具体.
如何开始将Python变量传递到我的批处理文件中?
我有一个 Python 脚本。我们称之为controller.py。我想使用controller.py运行另一个 Python 脚本并将几个变量传递给它。让我们调用第二个脚本分析器.py。
在不将Analyzer.py作为模块导入的情况下执行此操作的最佳方法是什么?以及如何在该脚本中引用我传递给analyzer.py的变量?
这是我使用子流程的失败尝试:
控制器.py
import subprocess
var1='mytxt'
var2=100
var3=True
var4=[['x','y','z'],['x','c','d']]
var5=r"C:\\Users\\me\\file.txt"
myargs=var1,var2,var3,var4,var5
my_lst_str = ' '.join(map(str, myargs))
my_lst_str ='python analyzer.py '+my_lst_str
subprocess.call(my_lst_str,shell=True)
Run Code Online (Sandbox Code Playgroud)
分析器.py
print 'Argument List:', str(sys.argv)
Run Code Online (Sandbox Code Playgroud)
我在 Stack Overflow 上看过类似的问题。我尝试过的一个经常推荐的解决方案是将analyzer.py 作为模块导入,但analyzer.py 定义了许多不同的函数。将其用作模块会创建许多嵌套函数,并且在这些嵌套函数中管理变量的范围很麻烦。
我需要为这些脚本使用 Python 2。我在 Windows 10 机器上。
我正在尝试用Python编写一个简单的程序,它从我的Downloads文件夹中获取所有音乐文件并将它们放入我的Music文件夹中.我正在使用Windows,我可以使用cmd提示移动文件,但是我收到此错误:
WindowsError: [Error 2] The system cannot find the file specified
这是我的代码:
#! /usr/bin/python
import os
from subprocess import call
def main():
os.chdir("C:\\Users\Alex\Downloads") #change directory to downloads folder
suffix =".mp3" #variable holdinng the .mp3 tag
fnames = os.listdir('.') #looks at all files
files =[] #an empty array that will hold the names of our mp3 files
for fname in fnames:
if fname.endswith(suffix):
pname = os.path.abspath(fname)
#pname = fname
#print pname
files.append(pname) #add the mp3 files to our array
print files …Run Code Online (Sandbox Code Playgroud) python ×9
subprocess ×6
batch-file ×1
macos ×1
popen ×1
python-2.x ×1
python-3.6 ×1
python-3.x ×1
tcpdump ×1
windowserror ×1