Python Exercises (i)

Source: Internet
Author: User

Write a file in Python that lists the current directory and all subdirectories, and print out the absolute path



#!/usr/bin/env python

Import OS

For root,dirs,files in Os.walk ('/tmp '):

For name in Files:

Print (Os.path.join (root,name))



Os.walk ()

Prototype: Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false)

We generally only use the first parameter. (Topdown indicates the order of traversal)
This method returns a ternary group for each directory (Dirpath, Dirnames, filenames). The first is the path, the second is the directory below the path, and the third is a non-directory below the path (for Windows, the file)

os.listdir (path)

The parameters have the following meanings. Path to get the paths to the content directory



Second, write the program to print the triangle


#!/usr/bin/env python

input = Int (raw_input (' Input number: '))

For I in range (input):

For j in Range (i):

print ' * ',

print ' \ n '



Third, the guessing device, the program randomly generates a bit number, and then wait for the user input, enter the number and generate the same number is considered successful. The triangle is printed successfully. Failure is re-entered (hint: random number function: randomness)


#!/usr/bin/env python

Import Random

While True:

input = Int (raw_input (' Input number: '))

Random_num = Random.randint (1, 10)

Print Input,random_num

if input = = Random_num:

For I in range (input):

For j in Range (i):

print ' * ',

print ' \ n '

Else

print ' Please input number again '


Python Exercises (i)

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.