The Art of Reading

Source: Internet
Author: User

1. Open the file () and read it using read ().

The following example reads two files, respectively!

Reference: Exercise-14

# Coding:utf-8

From sys import ARGV # import variable parameter

script, filename = argv # unpacking variable arguments to script and filename two variables/arguments

Print u "Open File ..." # Print as Unicode output

FILE = open (filename) # is output by default in read-only form, which is open (filename, ' R ')

Print File.read () # Remember "Print" Oh!

Print U "Please enter another file name ..."

Other_file = Raw_input (">") # raw_input () for user input

Other_txt = open (Other_file)

Print Other_txt.read ()

---------------------------------------------------------------------------------

2. Another way is to create a new function with Def

# Coding:utf-8

def read (): # def: (define) definition, new function

Print U "Please enter file name:"

filename = raw_input (">")

Print u "Open file%s ..."% file

TXT = open (filename)

Print Txt.read ()

Read ()

Read ()

---------------------------------------------------------------------------------

3. Of course, we can also use the For Statement loop and DEF statements to combine

# Coding:utf-8

def read ():

For number in range (1, 3):

Print U "Please enter file%s:"% number

filename = raw_input (">")

Print U "file is open ..."

TXT = open (filename)

Print Txt.read ()

Read ()

The Art of Reading

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.