'How to split a text file in multiple files?
I extracted a text file with the following format:
user1
$ANSIBLE_VAULT;1.1;AES256
33643334383262616539323337343966353463303464626331396638373435376565383963366463
3037383439333665636136653165613631336538313662660a656132393966613839373563636463
31356165663036643535306563393663363462316661613435313133666234656361326433356366
6637623462316363360a343066323363626633383631303032633334626365353734363862663332
3636
user2
$ANSIBLE_VAULT;1.1;AES256
65663365353466313564306261666634316137343438623432353030343832326632346333663332
6232393236626563326630383639636337346465386664320a663363643836663266613366313030
31313836336461303863366338613236323631336264363632316561646663663431306633333036
6562633430656630390a323034616232353430396261303436623037663665363736613530336431
6135
user3
$ANSIBLE_VAULT;1.1;AES256
35346138316166343430643434396566343761666361343935386464343638376532343566356331
6134393664646666626637643035373961613931353334360a626565336466316336313863313466
64383061323966316261656231613538663162666236386136396430663433343134303131326434
3461666634333437350a613766636361393236633262613739653962643732643135363561333534
6332
user4
$ANSIBLE_VAULT;1.1;AES256
62633764663030333566373434303632343030353163363661303066636431383936306264373235
3266316631383930303832643265316630353236323437350a356338316261616137633632333961
62646234363237383838366361383235393638376239386264333361373438616530323338656436
3836373463386663370a356164383538643131373337376638656236633630313433326362333334
6139
I need to split the file by each newline to receive files which are named after the user(1,4) and only contain the ansible secret.
What I tried:
from sys import argv
with open(argv[1], "r") as file:
for line in file:
fname = ""
secret = []
if line.startswith("user"):
fname = line
elif line.startswith("\\$ANSIBLE"):
secret.append(line)
elif line[0].isdigit():
secret.append(line)
elif len(line.strip()) == 0:
f = open(fname, "x")
for i in secret:
f.write(i)
f.close()
else:
print("This is not supposed to happen.)"
I get a SyntaxError: unexpected EOF while parsing so far and feel like this is the most inefficient method possible.
Solution 1:[1]
print("This is not supposed to happen.)"
# ^ typo here!
@JANO found it but posted in comments.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dekuShrub |
