本文共 5424 字,大约阅读时间需要 18 分钟。
#!/usr/bin/python print "Python is really a great language,", "isn't it?";
Python is really a great language, isn't it?
raw_input
input
#!/usr/bin/python str = raw_input("Enter your input: ");print "Received input is : ", str
Enter your input: Hello PythonReceived input is : Hello Python
#!/usr/bin/python str = input("Enter your input: ");print "Received input is : ", str
Enter your input: [x*5 for x in range(2,10,2)]Recieved input is : [10, 20, 30, 40]
file object = open(file_name [, access_mode][, buffering])
file_name:file_name变量是一个包含了你要访问的文件名称的字符串值。
access_mode:access_mode决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。
buffering:如果buffering的值被设为0,就不会有寄存。如果buffering的值取1,访问文件时会寄存行。如果将buffering的值设为大于1的整数,表明了这就是的寄存区的缓冲大小。如果取负值,寄存区的缓冲大小则为系统默认。
模式 | 描述 |
---|---|
r | 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。 |
rb | 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。 |
r+ | 打开一个文件用于读写。文件指针将会放在文件的开头。 |
rb+ | 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。 |
w | 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
wb | 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
w+ | 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
wb+ | 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
a | 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
ab | 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
a+ | 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。 |
ab+ | 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。 |
属性 | 描述 |
---|---|
file.closed | 返回true如果文件已被关闭,否则返回false。 |
file.mode | 返回被打开文件的访问模式。 |
file.name | 返回文件的名称。 |
file.softspace | 如果用print输出后,必须跟一个空格符,则返回false。否则返回true。 |
#!/usr/bin/python # 打开一个文件fo = open("foo.txt", "wb")print "Name of the file: ", fo.nameprint "Closed or not : ", fo.closedprint "Opening mode : ", fo.modeprint "Softspace flag : ", fo.softspace
Name of the file: foo.txtClosed or not : FalseOpening mode : wbSoftspace flag : 0
fileObject.close();
#!/usr/bin/python # 打开一个文件fo = open("foo.txt", "wb")print "Name of the file: ", fo.name # 关闭打开的文件
Name of the file: foo.txt
fileObject.write(string);
#!/usr/bin/python # 打开一个文件fo = open("/tmp/foo.txt", "wb")fo.write( "Python is a great language.\nYeah its great!!\n"); # 关闭打开的文件fo.close()
Python is a great language.Yeah its great!!
fileObject.read([count]);
#!/usr/bin/python # 打开一个文件fo = open("/tmp/foo.txt", "r+")str = fo.read(10);print "Read String is : ", str# 关闭打开的文件fo.close()
Read String is : Python is
#!/usr/bin/python # 打开一个文件fo = open("/tmp/foo.txt", "r+")str = fo.read(10);print "Read String is : ", str # 查找当前位置position = fo.tell();print "Current file position : ", position # 把指针再次重新定位到文件开头position = fo.seek(0, 0);str = fo.read(10);print "Again read String is : ", str# 关闭打开的文件fo.close()
Read String is : Python isCurrent file position : 10Again read String is : Python is
os.rename(current_file_name, new_file_name)
#!/usr/bin/pythonimport os # 重命名文件test1.txt到test2.txt。os.rename( "test1.txt", "test2.txt" )
os.remove(file_name)
#!/usr/bin/pythonimport os # 删除一个已经存在的文件test2.txtos.remove("text2.txt")
os.mkdir("newdir")
#!/usr/bin/pythonimport os # 创建目录testos.mkdir("test")
os.chdir("newdir")
#!/usr/bin/pythonimport os # 将当前目录改为"/home/newdir"os.chdir("/home/newdir")
os.getcwd()
#!/usr/bin/pythonimport os # 给出当前的目录os.getcwd()
os.rmdir('dirname')
#!/usr/bin/pythonimport os # 删除”/tmp/test”目录
转载地址:http://wssab.baihongyu.com/