(1)首先在桌面建立一个python文件,注意Python文件最好以.py命名。
(2)建立test.py文件,内容如下:
(3)运行程序。进入建立的test.py文件夹,然后在powershell 运行如下代码:python test.py
print ("""
I am a chinese.
I am very Happy!
I want to go home.
I hope someone else will accompany me.
""")
转义字符 功能
\\ 反斜杠(\)
\' 单引号(')
\" 双引号(")
\a ASCII 响铃符
\r ASCII 回车符
\t ASCII 水平制表符
\n ASCII 换行符
…
源代码:
from sys import argv
script,first,second,third = argv
print ("The Script is called:",script)
print ("your first variable is:",first)
print ("your second variable is:",second)
print ("your third variable is:",third)
运行时:
from sys import argv
script ,filename = argv
txt = open(filename)
print ("Here's your file %r:" %filename)
print (txt.read())
print ("Type the filename again:")
file_again = input("> ")
txt_again = open (file_again)
print (txt_again.read())
(2)read——读取文件内容,你可以把结果赋给一个变量
(3)readline——读取文本文件中的一行
(4)truncate——清空文件,请小心使用该命令
(5)write(stuff)——将stuff写入文件
from sys import argv
script,filename = argv
print ("We're going to erase %r." % filename)
print ("If you don't want that ,hit CTRL-c(^c).")
print ("If you want to do that,hit RETURN")
input("?")
print ("Opening the file...")
target = open (filename,'w')
print ("Truncating the file. Goodbye!")
target.truncate();
print ("Now I'm going to ask you for three lines.")
line1 = input ("line1:")
line2 = input ("line2:")
line3 = input ("line3:")
print ("I'm going to write these to the file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print ("And finally,we close it.")
target.close()
target.truncate() 是清空的意思,与“w”模式并不冲突,也并非后置条件。
from sys import argv
from os.path import exists
script , from_file ,to_file = argv
print ("Copying from %s to %s" % (from_file,to_file))
#we could do these two on one file too,how ?
in_file = open(from_file)
indata = in_file.read()
print ("The input file is %d bytes long " % len(indata))
print ("Dose the output file exist? %r" % exists(to_file))
print ("Ready,hit RETURN to cotinue,CTRL+C to abort.")
input()
out_file = open(to_file,"w")
out_file.write(indata)
print ("Alright, all done.")
out_file.close()
in_file.close()
这个命令将文件名字符串作为参数,如果文件存在的话,它将返回True;否则返回False.
import调用:from os.path import exists