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
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.