root@ubuntu:~/dongbo/tools/python/day2# vi diedai.py #!/usr/bin/env python #_*_ coding:utf-8 _*_ for i in "test.txt": print i,
root@ubuntu:~/dongbo/tools/python/day2# python diedai.py t e s t . t x t root@ubuntu:~/dongbo/tools/python/day2#
可以發現是將test.txt打印出來,沒有去查找test.txt文件。 下面是將文件內容打印出來了。 root@ubuntu:~/dongbo/tools/python/day2# cat diedai.py #!/usr/bin/env python #_*_ coding:utf-8 _*_ f = file("/root/dongbo/tools/python/day2/test.txt") for i in f: print i, root@ubuntu:~/dongbo/tools/python/day2#
root@ubuntu:~/dongbo/tools/python/day2# python diedai.py wo ai ni chenxiaoyandongbo dongbo ai chenxiaoyan
下面的方式就是把每個字符打印出來了。
root@ubuntu:~/dongbo/tools/python/day2# cat diedai.py #!/usr/bin/env python #_*_ coding:utf-8 _*_ f = file("/root/dongbo/tools/python/day2/test.txt") for i in f.readline(): print i, root@ubuntu:~/dongbo/tools/python/day2# python diedai.py w o a i n i c h e n x i a o y a n d o n g b o root@ubuntu:~/dongbo/tools/python/day2#