Read a file line by line and check line separator : File Utilities « File « Python






Read a file line by line and check line separator


import sys
if __name__ == "__main__":
         if len(sys.argv) > 1:
                 try:
                        f = open(sys.argv[1], "rb")
                 except:
                        print "No file named %s exists!" % (sys.argv[1],)
                 while 1:
                        t = f.readline()
                        if t == '':
                            break
                        if '\n' in t:
                            t = t[:-1]
                        if '\r' in t:
                            t = t[:-1]
                        sys.stdout.write(t + '\n')
                 f.close()



           
       








Related examples in the same category