目前,我有一个纯文本文件A,如
lowest priority
very high significance.
outstanding
very novel
在这个文件中,每一行都包含一个句子。我想将这个文件分成多个文件,每个文件由原始文件A的一行组成。例如,对于示例文件A,我想生成四个文件:
A1,它有一行,lowest priority
A2,它有一行,very high significance
A3,它有一行,outstanding
A4,它有单行,very novel。
如何在linux下做到这一点?
我试图创建一个函数,返回输出,因为命令将写入或Linux中的命令行
示例:
def cmd_simulator(commands):
#some code
cmd_simulator("date")
- Thu Jan 28 12:18:05 EST 2016
或Windows:
cmd_simulator("date")
- The current date is: Thu 01/28/2016
- Enter the new date: (mm-dd-yy)
我正在尝试跟踪我能找到的关于subprocess.Popen的信息,因为我想进行一个linux命令行调用。我尝试如下所示,但得到错误"Errno 2没有这样的文件或目录“。我没有试图打开一个文件,所以我不理解这个错误,当我使用常规的os.popen时,它工作得很好(尽管有其他问题涉及到当我不想要的时候等待进程完成)。
我似乎不知道如何正确地做这件事,任何建议都是值得感谢的。
编辑:我正在使用的命令是复杂和复杂的,在这里包含它太脱离上下文了,我想只要说当我使用os.popen而不是当我使用新的方式时代码可以工作就足够了,所以不,"linux命令行调用“显然不是我使用的调用。
su
我可以在Linux的命令行中正常运行:
$ tar c my_dir | md5sum
但是当我尝试用Python调用它时,我得到了一个错误:
>>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True)
<subprocess.Popen object at 0x26c0550>
>>> tar: You must specify one of the `-Acdtrux' or `-
代码:
# cat mylinux.py
# This program is to interact with Linux
import os
v = os.system("cat /etc/redhat-release")
输出:
# python mylinux.py
Red Hat Enterprise Linux Server release 7.6 (Maipo)
在上面的输出中,无论我为存储输出而定义的变量如何,都会显示命令输出。
如何仅使用os.system方法将shell命令输出存储到变量?
如何在Python函数中执行linux命令?我将在一个基于linux的服务器上运行python文件,在一些函数中,我希望有如下内容: def function():
#execute some commands on the linux system, eg. python /path1/path2/file.py
# Or execute a shell script, eg. /path1/path2/file.sh 要实现这一点,我需要什么python模块? 提前谢谢。
我正在做一个小程序,我可以用它的默认编辑器从电脑的任何地方打开一个文件。这是我的代码:
from os import *
import subprocess
print("Welcome to my File Finder. Here you can search for a file and open it.")
file_name = str(input("Your file's name:"))
print(subprocess.call(["xdg-open"], file_name))]
但它并没有打开,而是返回以下错误:
Tr
我希望使用python代码确定我的路由器的地址。我发现在安装了netifaces.gateways()库之后,我可以使用这个方法netifaces。
但我的问题是:
rimeh@rimeh-PC:~$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>