Reputation: 1
I am trying to replace some text in a file with python 2.6
However, it returns an extra newline here is my code:
for line in fileinput.input('/etc/php5/apache2/php.ini', inplace=True):
replace = re.sub(r'(post_max_size =).[0-9]+(M)', r'\1 64\2', line)
print replace
Input:
post_max_size = 6M
sdfsdfsd
post_max_size = 4M
sdfsdf
post_max_size = 164M
dfsdfsdfsdfsdf
output:
post_max_size = 64M
sdfsdfsd
post_max_size = 64M
sdfsdf
post_max_size = 64M
dfsdfsdfsdfsdf
Upvotes: 0
Views: 150
Reputation: 174662
The extra output you are seing is from print
, if you write the file back out, you should not see the extra line breaks.
Try it with this: print replace,
This is a minor point, but you should also pick a different name, as replace
is the name for a method in the standard library for strings.
Upvotes: 1
Reputation: 44394
Try:
print replace,
instead. The trailing comma suppresses the newline (but adds a trailing space). For complete control use the print function from __future__
instead.
Upvotes: 0