💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
先上写干货,几个开源网站: - [github.com](#) - [launchpad.net](#) - [gitorious.org](#) - [sourceforge.net](#) - [freecode.com](#) 今天介绍一下python函数和文件读写的知识。 ### 函数 ### ~~~ def print_two(*args):#That tells Python to take all the arguments to the function and then put them in args as a list arg1,arg2=args print "arg1: %r, arg2: %r"%(arg1,arg2) def print_two_again(arg1,arg2): print "arg: %r,arg: %r"%(arg1,arg2) def print_one(arg1): print "arg1: %r" %arg1 def print_none(): print "I got nothin'." return ; print_two("zed","shaw") print_two_again("zed","shaw") print_one("First!") print_none() ~~~ ~~~ def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates start_point = 10000 beans, jars, crates = secret_formula(start_point) ~~~ ### 文件读写 **读写取方法:**  **read()** 方法用来直接读取字节到字符串中, 不带参数表示全部读取,参数表示读取多少个字节  **readline()** 方法读取打开文件的一行,如果提供参数表示读取字节数,默认参数是-1,代表行的结尾  **readlines()**方法会读取所有(剩余的)行然后把他们作为一个字符串列表返回。可选参数代表返回的最大字节大小。 **输出方法:**    **write()** 方法表示写入到文件中去    **writelines()**  方法是针对列表的操作,接受一个字符串列表作为参数,写入文件。行结束符不会自动加入。 **核心笔记:**使用输入方法read() 或者 readlines() 从文件中读取行时,python并不会删除行结尾符。    **文件内移动:**    **seek()** 方法,移动文件指针到不同的位置。     **tell()** 显示文件当前指针的位置。 **文件内建属性**    file.name       返回文件名(包含路径)      file.mode       返回文件打开模式      file.closed      返回文件是否已经关闭      file.encoding   返回文件的编码 ~~~ input_file=raw_input("input_file: ") def print_all(f): print f.read() def rewind(f): f.seek(24)#seek 24 characters def print_a_line(line_count,f): print line_count,f.readline() current_file=open(input_file) print "First let's print the whole file: \n" print_all(current_file) print "Now let's rewing,kind of like a tape." rewind(current_file) print" Let's print three lines: " current_line = 1 print_a_line(current_line,current_file) current_line=current_line+1 print_a_line(current_line,current_file) current_line=current_line+1 print_a_line(current_line,current_file) ~~~ 如果文件是配置文件,可以用下面的代码来调用: ~~~ for line in f.readlines(): print(line.strip()) # 把末尾的'\n'删掉 ~~~ 更详细的介绍请点击:[http://www.cnblogs.com/NNUF/archive/2013/01/22/2872234.html](http://www.cnblogs.com/NNUF/archive/2013/01/22/2872234.html)