I have a series of files named file_0001.csv
, file_0002.csv
, ... file_1000.csv
etc. I need to read them iteratively by creating a list of the filenames and as
import numpy as np
import pandas as pd
for fileName in files:
data = pd.read_csv("folder" fileName)
data = data.values
How do I create the list of file names by checking the first and last file.
Thank you for your help
CodePudding user response:
if you are trying to create the specific file names, you can loop over from 1 to 1000 and create filenames
import numpy as np
import pandas as pd
for i in range(1,1001):
num = str(i)
filename = "file_" num.zfill(4) ".csv"
#data = pd.read_csv("folder" fileName)
#data = data.values
print filename
output last 10 lines:
file_0991.csv
file_0992.csv
file_0993.csv
file_0994.csv
file_0995.csv
file_0996.csv
file_0997.csv
file_0998.csv
file_0999.csv
file_1000.csv
CodePudding user response:
You should use pathlib
from the standard library. Then you can simply do
import pandas as pd
from pathlib import Path
folder_path = Path("path/to/folder")
for file in folder_path.glob("file_*.csv"):
print(file.name) # just to check
data = pd.read_csv(file)
# do something with data