Python Write txt

Source: Internet
Author: User

Python writes TXT, previously written by Jason, and write txt a little different, recorded.

Import Osdef Touch (path): U = ' u1= ' with open (path, ' W ') as F:f.write (U) f.write (' \ t ') f.write (U1) #os. Utime (Path, None) path = "CreativeFile.txt" Touch (Path)

1. There are several modes of opening (transfer from

Mode is open, and the optional value is R w a U, which represents read (default) write to add patterns that support various line breaks. If you open the file in W or a mode, it will be created automatically if the file does not exist. In addition, when you open an existing file in W mode, the contents of the original file will be emptied, because the operation of the first file is marked at the beginning of the file, and the writing operation will undoubtedly erase the original content. For historical reasons, newline characters in different systems have different modes, such as in Unix is A/n, and in Windows is '/r/n ', in the U-mode to open the file, is to support all the newline mode, it is said that '/R '/n '/'/r/n ' can represent line break, A tuple is used to store the newline characters used in this file. However, although there are multiple modes for line breaks, read the unified use of/n instead in Python. After the pattern character, you can also add a + B t these two identities, respectively, can be read and write to the file and in binary mode, text mode (the default) to open the file.

2. Note that Python cannot write int, it must be string. Http://

The explanation is:

Write () only takes a single string argument, so could does this:


outf.write(‘{}‘.format(num)) # more "modern"outf.write(‘%d‘ % num) # deprecated mostly

Also Note that would not be write append a newline to your output so if you need it supply it has to yourself it.


Using string formatting would give you + control over your output, so for instance you could write (both of these is E Quivalent):

num = 7outf.write(‘{:03d}\n‘.format(num))num = 12outf.write(‘%03d\n‘ % num)

To get three spaces, with leading zeros for your integer value followed by a newline:


Format () would be around-a long while, so it ' s worth learning/knowing.


Python Write txt

Related Article

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: 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.