Python simulation for Linux system Unix2dos function

Source: Internet
Author: User

Goal

1.Linux system and Windows System file system line break problems

2. Convert UNIX files to DOS file format

1.Linux system and Windows System file system line break

? About Carriage Return | The origin of the newline

Before the computer appeared, there was a telex typewriter (teletype Model 33), which could play 10 characters per second. However, it has a problem, that is, to finish a line to break the time, to use 0.2 seconds, just can play two characters. If there are new characters coming in these 0.2 seconds, the new character will be lost. So, the developers think of a way to solve this problem, is to add two after each line to end the character. One is called "carriage return", which tells the typewriter to position the printhead at the left border, and the other is called "line break", telling the typewriter to move the paper down one line. This is the carriage return | The origin of the newline

? Carriage Return | NewLine identifier problem

Later, after the computer appeared, carriage return and line feeds were applied to the computer, but because the price of the register is very expensive, scientists think that the end plus 2 characters is too wasted space, a character can be resolved, there is disagreement at this time. UNIX-like systems use a carriage return to identify a carriage return ' \ n ', and the Windows system continues to use carriage return line to identify the carriage return ' \ r \ n '

? problem

Unix-like files are copied to the Windows system, you lose the newline ID

Windows files are copied to Unix-like systems, the "^M" flag appears

2. Convert UNIX files to DOS file format

The code is as follows:

[email protected] python]# cat u2d.py

#!/usr/bin/env python#Coding:utf8ImportSYSdefUnix2dos (fname): Src_file=fname Dst_file= fname +'. DOS'Src_fobj=Open (src_file) dst_fobj= Open (Dst_file,'W')     forLineinchSrc_fobj:dst_fobj.write (Line.rstrip ('\ r \ n') +'\ r \ n') Src_fobj.close () dst_fobj.close ()if __name__=="__main__": Unix2dos (sys.argv[1])

? Run code, test effect

ls 1. *1. py  1ls1. t*11   ls1. t*1. txt  1. Txt.dos

* Tip: You can take the converted files to the Windows system to open to see if it is normal.

Python simulation for Linux system Unix2dos function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.