We should forget about small efficiencies — Donald Knuth
11 Aug
Although mordem md5sum(1) of coreutils in Linux can acknowledge the md5sum of FreeBSD format, I still want to convert FreeBSD format to Linux one via python. Here is original file:
md5sum generated by md5(1) of FreeBSD
MD5 (Thunderbird-1.5.0.4.exe) = 3b862cd35571ecec3de5e34112bf3f15
MD5 (Thunderbird-1.5.0.5.exe) = 3b93bd5529e2a81054514f57dc4b3916
And I want to convert to:
md5sum generated by md5sum(1) of Linux
3b862cd35571ecec3de5e34112bf3f15 Thunderbird-1.5.0.4.exe
3b93bd5529e2a81054514f57dc4b3916 Thunderbird-1.5.0.5.exe
The sample code is quite simple:
# Open file named md5sum.txt
hFile = open ("md5sum.txt", "rU")
# Read whole content into a List file handle
aListLines = [szLine for szLine in hFile]
# Loading completed, then close file handle
hFile.close()
# Reset output list
aListOutputLines = []
# Process each line one by one
for szLine in aListLines:
....# split 'szFile = szHash' by three charactes of ' = '
....szFile, szHash = szLine.split(' = ')
....# Strip what we don't need and add two characters of space
....aListOutputLines.append(szHash.rstrip('\n') \
........+ ' ' * 2 \
........+ szFile.lstrip('MD5 (').rstrip(')'))
# Now aListOutputLines is final result, hence we open file to write next
hFile = open("md5sum.txt.new", "w")
# Process each line one by one
for szLine in aListOutputLines:
....hFile.write("%s\n" % szLine)
# Closing a handle is a good habit, it's unnecessary though
hFile.close()
It’s all ;-)
By the way, the meaning of open (”md5sum.txt”, “rU”) is called open file with Universal Newline.
Popularity: 17% [?]
2 Responses for "[Python] Sample program to translate file into another"
In FreeBSD, use “md5 -r” will generate…
orz….
Leave a reply