I would like to add one space before and after each string in Python. I have 851 files. The first file contains 219 lines. The last file contains 1069 lines. Some line are just dots while other lines are numbers. I would like to use the center() function. I tried:
import os, os.path
for x in range(1, 852):
input_file_name = f"9.{x}.txt"
output_file_name = os.path.join(f"10.{x}.txt")
with open(input_file_name) as input_file:
with open(output_file_name, "w") as output_file:
for input_line in input_file:
output_line = input_line.center(2)
output_file.write(output_line)
This does not add any space. I want one space before each string and one space after each string.
Input
.
.
.
.
.
.
.
25
.
.
.
.
55
Expected output
x.x
x.x
x.x
x.x
x.x
x.x
x.x
x25x
x.x
x.x
x.x
x.x
x55x
NB : x stands for space. Any help would be appreciated. Thanks.