0531- exercises os.system

# 19, the ip.txt a, which is a per line ip, implement a function, ip ping results for each of the results to the record stored in ping.txt format ip: 0 or ip: 1, 0 Representative ping success, failure represents ping 
'' ' 
DEF ping_ip (source_dir): 
    Import OS 
    with Open (source_dir, "R & lt") AS FP: 
        ip_list = fp.readlines () 
    Result = [] 
    for IP in ip_list: 
        IF the os.system ( "of ping" + "" + IP) == 0: 
            result.append (ip.strip () + ":" + "0" + "\ n-") 
        elif the os.system ( "of ping" + "" + IP) 1 ==: 
            result.append (ip.strip () + ":" + "1" + "\ the n-") 
    with Open ( "C: \\ \\ the Dell Desktop the Users \\ \\ \\ exercises \\ 5 ping.txt "," W ", encoding =" UTF-. 8 ") AS FP: 
        fp.write ("".join(result))
    return result
print(ping_ip("C:\\Users\\dell\\Desktop\\练习\\5\\ip.txt"))


# Pause and prompt the "enter any character to continue" to continue pressing the Enter key, until the end of the file. '' ' 
Import OS

   


the os.path Import 
Import Time 
DEF the read_file (source_file): 
    Import OS 
    with Open (source_file, "R & lt", encoding = "UTF-. 8") AS FP: 
        Content = fp.readlines () 
    COUNT =. 1 
    for Line in Content: 
        Time .sleep (. 1) 
        IF COUNT == 0. 5%: 
            Data = iNPUT ( "input any character continued:") 
        Print (The time.strftime ( "% D%% Y-M-% H:% M:% S", time.localtime ()) + Line) 
        COUNT + 1 = 
Print (the read_file ( "C: \\ \\ Desktop Dell the Users \\ \\ \\ 0530.py practice. 5 \\")) 
'' ' 

# Problem 1: input line of characters, wherein each can count the number of numbers, letters, and other characters of the blank lines; 
'' ' 
string_data = iNPUT ( "Please enter his string:")
digit_num =0
letter_num =0
space_num =0
other_num =0
I in string_data for:
    i.isdigit IF (): 
        digit_num + =. 1 
    elif i.isalpha (): 
        letter_num + =. 1 
    elif i.isspace (): 
        space_num + =. 1 
    the else: 
        other_num + =. 1 
Print (digit_num, letter_num, space_num, other_num) 
 
DEF find_file_path (source_path):' ' 
# Problem 2: find s = a + aa + aaa + aaaa + aa .... aa value of s, where a is a number 
' ' 
DEF SUM (a, n-): 
    IF Not the isinstance (a, int): 
        return None 
    S = 0 
    for I in Range (. 1,. 1 n-+): 
        S + = int (STR (A) * I) 
    return S 
Print (SUM (2,3)) 
'' ' 
# Problem 3: printing a.txt document file path 
'' ' 
    Import OS 
    Import the os.path
    The os.path.split return (source_path) [0] 
Print (find_file_path ( "C: \\ \\ Desktop Dell the Users \\ \\ \\ practice the ip.txt. 5 \\")) 
'' ' 
' '' 
Import OS 
= result_list [] 
DEF find_file (path): 
    for the root, dirs, in os.walk Files (path, the topdown = False): 
        for Files in File: 
            IF "the ip.txt" in File: 
                result_list.append (the os.path. the Join (the root, file)) 
    return result_list 
Print (find_file ( "C: \\ \\ Desktop Dell the Users \\ \\ \\ practice the ip.txt. 5 \\")) 
'' ' 
# Problem 4: seeking a file path the number of all the documents 
' '' 
DEF count_file_num (path): 
    Import OS 
    Import OS.
    for i in os.listdir(path):
    os.chdir(path)
    file_count =0
        if os.path.isfile(path+"\\"+i):
            file_count +=1
    return file_count
print(count_file_num("C:\\Users\\dell\Desktop\\练习\\5"))
'''
#习题5: 找出txt文件的个数:    
'''
def count_txt_file_num(path):
    import os
    import os.path
    os.chdir(path)
    txt_file_count =0
    for i in os.listdir(path):
        if os.path.isfile(path+"\\"+i) and os.path.splitext(path+"\\"+i)[1]==".txt":
            txt_file_count +=1
    return txt_file_count
print(count_txt_file_num("C:\\Users\\dell\Desktop\\练习\\5"))
'''
#习题5:To view all files in the directory 

Import os'' '
path = "C: \\ Users \\ dell \ Desktop \\ Exercise 5 \\" 
os.chdir (path) 
for i in os.listdir (path): 
    Print (i) 
'' ' 
# Exercise 6: Find a the names of all files in the directory and subdirectories, without suffix 
'' ' 
Import os 
filename_list = [] 
( "C for root, dirs, in os.walk files: \\ \\ the Dell the Users \ Desktop \\ \\ exercise 5 ", the topdown = False): 
    for Files in File: 
        #Print (File) 
        filename_list.append (os.path.splitext (File) [0]) 
Print (filename_list) 
'' ' 
# Problem 7: Analyzing the input is a letter weeks few, if it is determined that the first letter of the second word of the same 
'' ' 
WEEK_DAY = { "m": "Monday", "tu": "Tuesday", "w": "Wednesday", "th": " Thursday "," f ":" Friday ""sa": "Saturday", "su": "Sunday"} 
Word the INPUT = ( 'Please enter the first letter of the week:') 
the Result = {}  
new_result = {}
for Key, value in week_day.items ():
    if key[0]==word:
        result[key]=value
if len(result)>1:
    two_word = input('请输入星期第二个字母:')
    for key,value in result.items():
        if key[1]==two_word:
            new_result[key]=value
    print(new_result)
else:
    print(result)
'''

 

Guess you like

Origin www.cnblogs.com/wenm1128/p/10979513.html