Reputation: 855
I am very new to Python and need help with a simple issue.
I have a text file "alias.txt" and need to iterate over the aliases in the file and create a new file (or stdout) where the text looks like this;
#!/bin/python3
import alias.txt
alias_str = "ma <alias>\nmda -a <alias> mail-relay <alias>@ant.domain.com\n"
print (alias_str.replace("<alias>", "<aliases from alias.txt>"))
So the output should be something like;
ma alias1
mda -a alias1 mail-relay [email protected]
ma alias2
mda -a alias2 mail-relay [email protected]
etc...
Where the alias file looks like;
alias1
alias2
alias3
etc...
Upvotes: 1
Views: 1265
Reputation: 3001
You would have to open and read the file, as opposed to importing it:
with open('alias.txt') as f:
for alias in f:
alias_str = "ma <alias>\nmda -a <alias> mail-relay <alias>@ant.domain.com\n"
print (alias_str.replace("<alias>", alias.strip()))
and if you want to create a new file, instead of just printing the lines:
with open('alias.txt') as f:
with open('alias2.txt', 'w') as f2:
for alias in f:
alias_str = "ma <alias>\nmda -a <alias> mail-relay <alias>@ant.domain.com\n"
f2.write(alias_str.replace("<alias>", alias.strip()))
Upvotes: 3