file 'with ... as'
This commit is contained in:
parent
e539d2a428
commit
5dec1eb382
7
links.py
7
links.py
|
@ -25,7 +25,7 @@ for filename in os.listdir():
|
|||
incoming[wikilink] = set()
|
||||
|
||||
# open file and search for all outgoing links
|
||||
file = open(filename)
|
||||
with open(filename) as file:
|
||||
pre_mode = False
|
||||
for line in file:
|
||||
if re.match("```",line) != None: # toggle preformatted mode
|
||||
|
@ -40,7 +40,6 @@ for filename in os.listdir():
|
|||
# add this file
|
||||
incoming[match].add(wikilink)
|
||||
line = line[m.end()+1:]
|
||||
file.close()
|
||||
|
||||
os.chdir('../tmp/')
|
||||
# remove incoming links for:
|
||||
|
@ -54,7 +53,7 @@ for key,links in incoming.items():
|
|||
# print(key)
|
||||
|
||||
# open file in append mode
|
||||
file = open(filename, 'a')
|
||||
with open(filename, 'a') as file:
|
||||
|
||||
if len(links):
|
||||
# write incoming links
|
||||
|
@ -62,9 +61,7 @@ for key,links in incoming.items():
|
|||
file.write("\n\n# incoming links\n")
|
||||
for link in links:
|
||||
gemlink = '=> ./{} {}'.format(wikilink2Filename(link), link)
|
||||
# print(gemlink)
|
||||
file.write(gemlink+"\n")
|
||||
else:
|
||||
print( '{}: orphan\n'.format(key) )
|
||||
file.write("\n\nno incoming links\n")
|
||||
file.close()
|
||||
|
|
Loading…
Reference in New Issue