python第四章课后习题(2)

设计一个函数fun2,将传入的字符串第一个#之前的所有内容(不包括#)写到file_out指定的文件中。

一样很简单

def fun2(s,file_out='inputchar.txt'):
    """
    Generate the file "inputchar.txt" with everything up to the first "#" in the input string.
    Arg:
        s : a string as input; e.g. "abcdefg#123"
        The content in inputchar.txt should be "abcdefg"
    """
    with open(abspath(file_out),'w',encoding='utf8') as f:
        f.write(s[:s.find("#")])

猜你喜欢

转载自blog.csdn.net/qq_53029299/article/details/114598027