So I'm trying to create a Python script that will look through *.styles
and *.layout
files and replace specific strings in those files. I've tried a number of things, but I can't figure out what I should be doing, and even looking at other code samples online just makes it even more confusing to understand.
here's what I have in my code currently;
#!/usr/bin/python3
# coding: utf-8
import os
import sys
import fileinput
f1 = open ('*.styles', 'r') # read *.styles
f2 = open ('*.styles', 'w') # write *.styles
f3 = open ('*.layout', 'r') # read *.layout
f4 = open ('*.layout', 'w') # write *.layout
for line in f1:
f2.write(line.replace('font-size=15','font-size=12')) # replace string for
f2.write(line.replace('font-size=14','font-size=12')) # files in *.styles
f2.write(line.replace('font-size=18','font-size=14'))
f2.write(line.replace('font-size=30','font-size=18'))
f2.write(line.replace('font-size=36','font-size=18'))
f2.write(line.replace('font-size=16','font-size=12'))
f2.write(line.replace('font-size=26','font-size=16'))
f2.write(line.replace('font-size=48','font-size=20'))
f2.write(line.replace('font-size=28','font-size=16'))
f2.write(line.replace('font-size=17','font-size=14'))
f2.write(line.replace('font-size=21','font-size=14'))
f1.close()
f2.close()
for line in f3:
f4.write(line.replace('font-size=15','font-size=12')) # replace string for
f4.write(line.replace('font-size=14','font-size=12')) # files in *.layout
f4.write(line.replace('font-size=18','font-size=14'))
f4.write(line.replace('font-size=30','font-size=18'))
f4.write(line.replace('font-size=36','font-size=18'))
f4.write(line.replace('font-size=16','font-size=12'))
f4.write(line.replace('font-size=26','font-size=16'))
f4.write(line.replace('font-size=48','font-size=20'))
f4.write(line.replace('font-size=28','font-size=16'))
f4.write(line.replace('font-size=17','font-size=14'))
f4.write(line.replace('font-size=21','font-size=14'))
f3.close()
f4.close()
print ("\n\n ## Done!\n\n")
sys.exit(0)
I did want to include a "Would you like to commit changes? [Y/n]" with a simple Yes/No user input answer, but that got a little too complicated to figure out with While True:
and While False:
statements.
When this code is test run on the files I want to test this on it just turns an error saying;
line 8, in <module>
f1 = open ('*.styles', 'r') # read *.styles
FileNotFoundError: [Errno 2] No such file or directory: '*.styles'
I just want to be able to run from command line like so;
$ string_search.py .
so it changes all files in the current directory that contains *.styles
and *.layout
files in that directory so you don't have to specify an actual directory, just in current directories and folders.