|||
# send information to the "Command Output"
print "hello world"
# creat a new file and write it
f = open('helloworld.txt','w') # creat a txt file in the working directory. w stands for "write". when using w, the program will overwite whatever might have been contained in the file.
f.write('hello world!!') # write "hello world" to the txt file.
f.close() # close the file.
f = open('/Users/Yangyang/Documents/acs_230715.dat','w') # creat a dat file in the given path.
f.write('hello world!!')
f.close()
# read a file
f = open('helloworld.txt','r') # open the txt file and read from it.
m = f.read() # copy the contents of the above txt file into m.
print m # send the information contained in m to the "Command Output"
f.close()
f = open ('/Users/Yangyang/Documents/acs_2_230715.dat','r') # open the dat file from a special path and read from it.
n = f.read() # copy the contents of the above dat file into n.
print n
f.close()
# append to a pre-existing file
f = open('helloworld.txt','a')
f.write('n'+'hello world') # 'n' stands for new line. add 'hello world' in the new line of the txt file.
f.close()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-11 03:57
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社