# coding:utf-8
import pandas as pd
import os
# 遍历时间所在的文件
path_time = "D:\\zpxPycharm\\pythonZTDmerge\\new"
for out_name,i in zip(os.listdir(path_time),range(len(os.listdir(path_time)))): # 得到指定路径下的文件名
for time_name in os.listdir(path_time+'\\'+out_name):
path = path_time+'\\'+out_name+'\\'+time_name
file = pd.read_csv(path,header = None, sep='delimiter')
if file.shape[0]>10:
file=file.drop(list(range(file.shape[0]-3621)))
file.to_csv(path,index=0,header=0)# 不保存pandas的索引和表头,保持txt文件的原格式
else:
file.to_csv(path,index=0,header=0)
下面是报错
D:\zpxPycharm\pythonZTDmerge\venv\Scripts\python.exe D:/zpxPycharm/pythonZTDmerge/lastrow.py
Traceback (most recent call last):
File "D:/zpxPycharm/pythonZTDmerge/lastrow.py", line 8, in <module>
for time_name in os.listdir(path_time+'\\'+out_name):
NotADirectoryError: [WinError 267] 目录名称无效。: 'D:\\zpxPycharm\\pythonZTDmerge\\new\\53797.txt'
Process finished with exit code 1
"D:\zpxPycharm\pythonZTDmerge\new"
建议这么去写路径
path_time = r"D:\zpxPycharm\pythonZTDmerge\new"
如果觉得答案对你有帮助,请点击下采纳,谢谢~