文本文件的内容作为python脚本的输入

时间:2014-03-27 11:23:05

标签: python python-2.7

我希望将文本文件的内容用作我的python脚本的输入。这是我的剧本。我想替换几个字:

import subprocess
import fileinput
import sys
import os

for line in text:
    line = line.replace("/dev/sda3              ", "")
    line = line.replace("/dev/sda6              ", "")
    line = line.replace("/dev/sda2              ", "")
    line = line.replace("/dev/sda1              ", "")
    line = line.replace("tmpfs                  ", "") 

任何建议都非常感谢。

1 个答案:

答案 0 :(得分:1)

with open ("data.txt", "r") as myfile:
    data=myfile.read().replace('\n', '')

查看How do I read a text file into a string variable in Python