1.读取文本文件
vi file_to_read.txt
I'm
already
much
better
at
Python
vi first_script.py
import sys
input_file=sys.argv[1] #第二个传输参数
filereader=open(input_file,'r') #读取方式打开
for row in filereader:
print(row.strip())
filereader.close()
#结果
[root@mysql51 ~]# python first_script.py file_to_read.txt
I'm
already
much
better
at
Python
[root@mysql51 ~]# python first_script.py "/root/file_to_read.txt"
I'm
already
much
better
at
Python
2.读取文件的新型语法
#with open(input_file,'r',newline='') as filereader: (语法错误)
#修改为:with open(input_file,'rb') as filereader:
import sys
from math import exp,log,sqrt
import re
from datetime import date,time,datetime,timedelta
from operator import itemgetter
input_file=sys.argv[1]
with open(input_file,'rb') as filereader: #newline无法识别,语法错误
for row in filereader:
print("{}".format(row.strip()))
#结果
[root@mysql51 ~]# python first_script.py "/root/file_to_read.txt"
I'm
already
much
better
at
Python
3.使用glob读取多个文本文件
vi another_file_to_read.txt
This
text
comes
from
a
differenct
text
file.
vi first_script.py
#encoding:utf-8
#导入os 模块使用 os.path.join
from math import exp,log,sqrt
import re
from datetime import date,time,datetime,timedelta
from operator import itemgetter
import sys
import glob
import os
inputPath=sys.argv[0]
for input_file in glob.glob(os.path.join(inputPath,'*.txt')):
with open(input_file,'rb') as filereader:
for row in filereader:
print("{}".format(row.strip()))
#结果
[root@mysql51 python_scripts]# python first_script2.py /root/python_scripts/
I'm
already
much
better
at
Python
This
text
comes
from
a
differenct
text
file.
[root@mysql51 python_scripts]# ll
total 16
-rw-r--r--. 1 root root 52 Mar 11 13:58 another_file_to_read.txt
-rw-r--r--. 1 root root 39 Mar 11 13:39 file_to_read.txt
-rw-r--r--. 1 root root 367 Mar 11 14:02 first_script2.py
-rw-r--r--. 1 root root 275 Mar 11 13:52 first_script.py
4.将结果写入到文件中。
#encoding:utf-8
from math import exp,log,sqrt
import re
from datetime import date,time,datetime,timedelta
from operator import itemgetter
import sys
import glob
import os
my_letters=['a','b','c','d','e','f','g','h','i','j']
max_index=len(my_letters)
output_file=sys.argv[1]
filewriter=open(output_file,'w')
for index_value in range(len(my_letters)):
if index_value<(max_index-1):
filewriter.write(my_letters[index_value]+'\t') #没到最后就输入制表符
else:
filewriter.write(my_letters[index_value]+'\n') #到了末尾就回车
filewriter.close()
print("Output written to file")
#结果
[root@mysql51 python_scripts]# python first_script.py /root/python_scripts/file_to_write.txt
Output written to file
[root@mysql51 python_scripts]# more file_to_write.txt
a b c d e f g h i j
5.写入CSV文件
vi first_script3.py
#encoding:utf-8
from math import exp,log,sqrt
import re
from datetime import date,time,datetime,timedelta
from operator import itemgetter
import sys
import glob
import os
my_numbers=[0,1,2,3,4,6,6,7,8,9]
max_index=len(my_numbers)
output_file=sys.argv[1]
filewriter=open(output_file,'a') #a 追加模式,r 读取模式,w 写入模式
for index_value in range(len(my_numbers)):
if index_value<(max_index-1): #出来最后一位。
filewriter.write(str(my_numbers[index_value])+',') #未到末尾写逗号
else:
filewriter.write(str(my_numbers[index_value])+'\n') #最后一个后面写回车
filewriter.close()
print("Output appended to file")
#结果
[root@mysql51 python_scripts]# python first_script3.py first_script3.out
Output appended to file
[root@mysql51 python_scripts]#
[root@mysql51 python_scripts]#
[root@mysql51 python_scripts]# more first_script3.out
0,1,2,3,4,6,6,7,8,9
6.总结
输入文件名需要sys模块。导入sys后可以使用argv[0]等表示参数。
该文演示了Python如何读取单个文本文件、使用新型语法读取、通过glob读取多个文本文件,以及将内容写入文件和CSV格式文件的方法。涉及sys模块参数传递、文件处理的with语句和不同文件模式。
689

被折叠的 条评论
为什么被折叠?



