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)