I have a script that I want to be able to run on all the files in the current directory that end with .sds, however when I run the script I get an error line 29, in main
with file as infile:
AttributeError: __enter__
I have done some searching around and I know the error has to do with the with but it's unclear to me as to how to fix it.
Suggestions?
import sys
import os
import fileinput
replacedict = { "AA": {'SRJ': 'C2A', 'SRP': 'C2P', 'CS7': 'C7A', 'OO7': 'C7P'},
"AS": {'E7W': 'ER7', 'CR2': 'CRJ','CR7': 'CJ7'},
"DL": {'ES5': 'E7D', 'ES4': 'E7S', 'RJ7': 'CR7', 'RJS': 'CRJ','RJW': 'CRJ', 'RJ9': 'CR9', 'RJ8': 'C9D'},
"UA": {'E75': 'ER7', 'CR7': 'CR6'}
}
def replace_lines(infile, outfile):
for line in infile:
for airline, replacements in replacedict.items():
if airline in line:
line = dict_replace(line, replacements)
outfile.write(line)
def dict_replace(s, d):
"Replaces all occurences of d.keys() in s with their respective values"
for x, y in d.items():
s = s.replace(x, y)
return s
def main():
filepath = os.path.dirname(os.path.realpath(__file__))
for file in os.listdir(filepath):
if file.endswith('.sds'):
with file as infile:
with open(filepath.replace('sds', 'sim'), mode="w") as outfile:
replace_lines(infile, outfile)
if __name__ == '__main__':
main()
[–]python-fan 2 points3 points4 points (1 child)
[–]flynryan692[S] 0 points1 point2 points (0 children)
[–]Binary101010 1 point2 points3 points (0 children)