Use python to read all the csv format data in the file in sequence

Use python to read all the csv format data in the file in turn:

#coding=gbk
import pandas as pd 
import os
path = r'D:\ml_datasets\PHM\c6' def get_file(): #创建一个空列表 files =os.listdir(path) files.sort() #排序 list= [] for file in files: if not os.path.isdir(path +file): #判断该文件是否是一个文件夹 f_name = str(file) # print(f_name) tr = '\\' #多增加一个斜杠 filename = path + tr + f_name list.append(filename) return list list = get_file() print('\\') print(list[:6]) data = pd.read_csv(list[2]) print(data.head())

Then you can cycle through the list and read each file.

The output is:

#输出为:
# \
# ['D:\\ml_datasets\\PHM\\c6\\c_6_001.csv', 'D:\\ml_datasets\\PHM\\c6\\c_6_002.csv', 'D:\\ml_datasets\\PHM\\c6\\c_6_003.csv', 'D:\\ml_datasets\\PHM\\c6\\c_6_004.csv', 'D:\\ml_datasets\\PHM\\c6\\c_6_005.csv', 'D:\\ml_datasets\\PHM\\c6\\c_6_006.csv']
#    -0.031  -1.071  0.361  -0.113  -0.124  -0.132  -0.005
# 0 0.030 -0.995 0.354 0.098 0.071 0.088 -0.005 # 1 0.141 -0.903 0.422 -0.040 -0.078 -0.059 -0.005 # 2 0.223 -0.822 0.419 -0.153 -0.161 -0.172 -0.004 # 3 0.354 -0.699 0.461 0.045 0.039 0.035 -0.005 # 4 0.408 -0.606 0.444 -0.031 -0.047 -0.035 -0.005

For some file names beginning with numbers, each file may not be output in sequence, and you can use the sort () method:

path = 'D:\ml_datasets\PHM\c6_new_train'
path_list=os.listdir(path)
for filename in path_list:
    print(os.path.join(path,filename))
# D:\ml_datasets\PHM\c6_new_train\1.csv
# D:\ml_datasets\PHM\c6_new_train\12.csv # D:\ml_datasets\PHM\c6_new_train\121.csv # D:\ml_datasets\PHM\c6_new_train\123.csv # D:\ml_datasets\PHM\c6_new_train\2.csv # D:\ml_datasets\PHM\c6_new_train\3.csv # D:\ml_datasets\PHM\c6_new_train\4.csv # D:\ml_datasets\PHM\c6_new_train\6.csv path_list.sort(key=lambda x:int(x[:-4])) #对读取的路径进行排序 for filename in path_list: print(os.path.join(path,filename)) # D:\ml_datasets\PHM\c6_new_train\1.csv # D:\ml_datasets\PHM\c6_new_train\2.csv # D:\ml_datasets\PHM\c6_new_train\3.csv # D:\ml_datasets\PHM\c6_new_train\4.csv # D:\ml_datasets\PHM\c6_new_train\6.csv # D:\ml_datasets\PHM\c6_new_train\12.csv # D:\ml_datasets\PHM\c6_new_train\121.csv # D:\ml_datasets\PHM\c6_new_train\123.csv 

Python file and directory operation method:

#coding=gbk
'''
Created on 2018年12月1日

@author: Administrator
'''
#python 文件和目录操作方法
import os 
# 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() # 2.返回指定目录下的所有文件和目录名:os.listdir() # 3.函数用来删除一个文件:os.remove() # 4.删除多个目录:os.removedirs(r“c:\python”) # 5.检验给出的路径是否是一个文件:os.path.isfile() # 6.检验给出的路径是否是一个目录:os.path.isdir() # 7.判断是否是绝对路径:os.path.isabs() # 8.检验给出的路径是否真地存:os.path.exists() # 9.返回一个路径的目录名和文件名:os.path.split() eg os.path.split('/home/swaroop/byte/code/poem.txt') 结果:('/home/swaroop/byte/code', 'poem.txt') # 10.分离扩展名:os.path.splitext() file_name = r'D:\liteide' print(os.getcwd()) # 获取当前工作目录 print(os.listdir(file_name)) # 返回指定目录下的所有文件和目录名, ['bin', 'CONTRIBUTORS', 'LGPL_EXCEPTION.TXT', 'lib', 'LICENSE.LGPL', 'README.md', 'share', 'test_remove.txt'] # os.remove(r'D:\liteide\test_remove.txt') #删除指定文件 # print(os.listdir(path)) print(os.path.isfile(file_name)) # False print(os.path.isdir(file_name)) #True 是一个目录 print(os.path.isabs(file_name)) #True 绝对路径 # 9.返回一个路径的目录名和文件名:os.path.split() print(os.path.split(r'D:\liteide\LGPL_EXCEPTION.TXT')) # ('D:\\liteide', 'LGPL_EXCEPTION.TXT') print(os.path.splitext(r'D:\liteide\LGPL_EXCEPTION.TXT')) #('D:\\liteide\\LGPL_EXCEPTION', '.TXT') # 11.获取路径名:os.path.dirname() print(os.path.dirname(file_name)) # D:\ # 12.获取文件名:os.path.basename() print(os.path.basename(file_name)) # liteide # 13.运行shell命令: os.system() print(os.system('cd D:\python1')) #0,返回0 ,表示可以使用 # 14.读取和设置环境变量:os.getenv() 与os.putenv() # 15.给出当前平台使用的行终止符:os.linesep Windows使用'\r\n',Linux使用'\n'而Mac使用'\r' print(os.linesep) # 16.指示你正在使用的平台:os.name 对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix' print(os.name) #nt # 17.重命名:os.rename(old, new) # print(os.rename(r'D:\liteide\old_name', r'D:\liteide\new_name')) #修改文件夹的名字 # 18.创建多级目录:os.makedirs(r“c:\python\test”) # print(os.makedirs(r'D:\liteide\hello_makedirs')) #创建了一个文件夹 # 19.创建单个目录:os.mkdir(“test”) # print(os.mkdir(r'D:\liteide\hah')) # 20.获取文件属性:os.stat(file) print(os.stat(file_name)) # 21.修改文件权限与时间戳:os.chmod(file) # 22.终止当前进程:os.exit() # 23.获取文件大小:os.path.getsize(filename) print(os.path.getsize(file_name)) # 4096

 

2. File operations

def test_file():
    file=r'D:\liteide\test.txt' with open(file, mode='r+', encoding='utf-8') as f: for line in f.readlines(200): #还可以设置读取的大小 print(line) f.writelines('append somthing') f.flush()
w:以写方式打开,
a:以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+:以读写模式打开
w+:以读写模式打开 (参见 w )
a+:以读写模式打开 (参见 a )
rb:以二进制读模式打开
wb:以二进制写模式打开 (参见 w )
ab:以二进制追加模式打开 (参见 a )
rb+:以二进制读写模式打开 (参见 r+ )
wb+:以二进制读写模式打开 (参见 w+ )
ab+:以二进制读写模式打开 (参见 a+ )

 

fp.read ([size]) #size is the length of the read, in bytes
fp.readline ([size]) #Read a line, if size is defined, it may return only part of a line
fp.readlines ([ size]) #Take each line of the file as a member of a list and return the list. In fact, its internal is realized by calling readline () in a loop. If the size parameter is provided, size is the total length of the read content, which means that it may be read only to a part of the file.
fp.write (str) #Write str to the file, write () does not add a newline character after str
fp.writelines (seq) #Write all the contents of seq to the file (multiple lines are written at once) Into). This function is also written faithfully, without adding anything after each line.
fp.close () #Close the file. Python will automatically close a file when it is not in use, but this feature is not guaranteed, it is best to develop the habit of closing. If a file is still operated after it is closed, ValueError
fp.flush () #Write the contents of the buffer to the hard disk
fp.fileno () #Return a long "file label"
fp.isatty () # Whether the file is a terminal device file (unix system)
fp.tell () #Return the current position of the file operation mark, with the beginning of the file as the origin
fp.next () #Return to the next line, and move the file operation mark to the next line. When a file is used in a statement such as for… in file, the next () function is called to traverse.
fp.seek (offset [, whence]) #Move the file operation marker to the position of offset. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, it is not necessary if the whenence parameter is provided, wherehence can be 0 to start from the beginning, and 1 to start from the current position. 2 indicates that the end of the file is used as the origin. Note that if the file is opened in a or a + mode, the file operation flag will automatically return to the end of the file each time a write operation is performed.
fp.truncate ([size]) #Cut the file to the specified size, the default is to cut to the position of the current file operation mark. If the size is larger than the size of the file, the file may not be changed according to the system, or the file may be filled to the corresponding size with 0, or it may be added with some random content.

3. Directory operation method

import shutil   
# 目录操作方法
# 1.创建目录
# os.mkdir("file") 
      
# os.mkdir(r'D:\liteide\mkdir_file') # 2.复制文件: # shutil.copyfile("oldfile","newfile") #oldfile和newfile都只能是文件 # shutil.copy("oldfile","newfile") #oldfile只能是文件夹,newfile可以是文件,也可以是目标目录 # 3.复制文件夹: # 4.shutil.copytree("olddir","newdir") #olddir和newdir都只能是目录,且newdir必须不存在 # 5.重命名文件(目录) # os.rename("oldname","newname") #文件或目录都是使用这条命令 # 6.移动文件(目录) # shutil.move("oldpos","newpos") # 7.删除文件 # os.remove("file") # 8.删除目录 # os.rmdir("dir") #只能删除空目录 # shutil.rmtree("dir") #空目录、有内容的目录都可以删 # shutil.rmtree(r'D:\liteide\mkdir_file') # shutil.rmtree(r'D:\liteide\hello_makedirs') # 9.转换目录 # os.chdir("path") #换路径 s = ['h', 'j', 'k'] t = '.'.join(s) print(t) # h.j.k print(t.split('.')) # ['h', 'j', 'k']

 

Guess you like

Origin www.cnblogs.com/junge-mike/p/12761518.html