-1

I want that the content of a text file to be used as a input for my python script. This is my script. I want to replace few words:

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                  ", "") 

Any suggestion is highly appreciated.

Abhishek dot py
  • 909
  • 3
  • 15
  • 31

1 Answers1

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

Take a look at How do I read a text file into a string variable in Python

Community
  • 1
  • 1
theaembee
  • 69
  • 3
  • This will replace words for each instance? – Abhishek dot py Mar 28 '14 at 08:13
  • I am not sure what you want, but it imports a text file into a string while replacing a part of it (in this case the line break (\n). It is replaced in the string not in the file as you can see from the 'r'-Option (read) in the open command. If you want to do that several times, than do it several times. – theaembee Mar 28 '14 at 12:25