Stupid methodology python (EX1-17)--%,input, unpack, read, write files

Source: Internet
Author: User
Tags unpack

Tips: alt+3 can be multi-line annotation alt+4 multiline uncomment, F5 run, cmd directly enter python-m Pydoc can open the Help document, or can be directly followed by a function can be queried using such as Python-m Pydoc round


1%r

Percent-semicolon unconventional usage:

Formatter = "%r%r%r%r"

Print Formatter% (1, 2, 3, 4)

Compare%r with%s. Have you noticed. %r prints what you write in the script, and%s prints what you should see.

2 input ()

There is no raw_input () function in the Python3, and the input () is used directly instead

You can add a hint in parentheses, such as: Age=input (' age? ')

3 parameters, Solution package (ex13)

From sys import ARGV
A,b,c,d = argv
Print (' 1 ', a)
Print (' 2 ', b)

Print (' 3 ', c)

Print (' 4 ', D)

Then open with >>>python 1.py first 2nd 3rd #要理解到1 at the command prompt. PY is actually a parameter

So you can "unpack the things in the argv and give all the arguments to the variable name on the left."

4 read files (ex15)

From sys import ARGV #调用sys的argv

Script,filename= argv #用argv获取文件名, script,filename only variable name does not affect the random

Txt=open (filename) #打开输入的第二个参数对应文件

Print (' Here is your file%r. '%filename) #读取第二个参数对应文件的名字
Print (Txt.read ()) #显示第二个参数对应文件的内容

Print (' Type the filename again: ') #进行第二种打开文件的方法

File_again=input (' > ') #让输入要打开文件的名字

Txt_again=open (File_again) #打开

Print (Txt_again.read ())

At the command line, enter the >>>python 1.py ex15_sample.txt #输入两个参数, replacing Script,filename respectively

5 The commands to be remembered are as follows:

close– closes the file. Save the file-> with your editor. a meaning.

read– Read the contents of the file. You can assign the result to a variable.

readline– reads a line in a text file.

truncate– Empty The file, please use this command carefully.

Write (stuff) – writes stuff to the file.

6 Writing Files

From sys import ARGV

Script,filename= argv# Get file name with argv
Print (' Wo are going to erase%r. '%filename)
Print (' If you don\ ' t want That,hit ctrl-c (^c). ')
Print (' If you don't want That,hit return ')

Input ('? ')

Print (' Opening the file ... ')
Target=open (filename, ' W ') #写入模式

Print (' Good bye! ')
Target.truncate () #清除内容

# # #前半段是擦除之前的数据, the second half is write data

Print (' Now I am going to ask 3 lines ')

Line1=input (' line1: ') # #此处引号不能去掉, because there is no definition of line1
Line2=input (' line2: ')
Line3=input (' Line3: ')

Print (' I am going to write this to the file. ')

Target.write (line1)
Target.write ("\ n")
Target.write (line2)
Target.write ("\ n")
Target.write (LINE3)
Target.write ("\ n")
###### #此处也可用target. Write (line1 + ' \ n ' + line2 + ' \ n ' + line3) instead, note that write () can only be a string. If multiple strings are used, the print () can be separated by commas, and the ##### #write () can only be added with each string


Print (' and finally, we close it. ')
Target.close ()


7 File Mode

When the default file is opened, it returns a read-only file, and if you want to write it in the file, you must display the definition mode parameter, which is the value Description ' r ' Read mode ' W ' write mode ' which is most commonly used by the mode parameter. A ' Append mode ' B ' Binary mode (added to the other mode) ' + ' Read/write mode (added to mode) where ' + ' can be added in any one mode Later, which means that readable writable ' B ' is used to change different file processing methods, b means binary, video, image, etc. information is a binary file that can be read with the ' RB ' mode, which reads the file in binary form.

8 ex17

From sys import ARGV
From Os.path Import exists

Script, from_file, to_file = argv

Print (' Copy from%s to%s '% (From_file, to_file)

Input=open (From_file)
Indata=input.read ()

Print (' The input file is%d bytes long '%len (indata))

Print (' Does the output ile exist?%r '%exists (to_file))
Print (' Ready,hit return to Continue,ctrl-c to abort. ')
Input ()

Output=open (To_file, ' W ')
Output.write (Indata)

Print (' All right! ')

Output.close ()
Input.close ()

######### #不知道哪里出错了, hint ' typeerror ' _io.textiowrapper ' object is not callable '

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.