import pandas as pd import os from pathlib import Path Set the directory where your CSV…
- User
import pandas as pd import os from pathlib import Path
Set the directory where your CSV files are located
base_directory_path = r"..\downloaded_files\Binance\test"
List of folders to iterate through
folders = [d for d in Path(base_directory_path).iterdir() if d.is_dir()]
Loop through each folder and then through each CSV file within them
for folder in folders: folder_path = Path(base_directory_path)/folder # Iterate over each file in the directory for filename in os.listdir(folder_path): if filename.endswith('.csv'): # Make sure to process only CSV files file_path = os.path.join(folder_path, filename)
code# Read the CSV file df = pd.read_csv(file_path, skiprows=1) # Rename the 8th and 9th columns # Note: Python is zero-indexed, so the 8th column is at index 7, and the 9th is at index 8. columns = df.columns.tolist() if len(columns) >= 9: # Check if there are enough columns columns[7] = 'volume_base' columns[8] = 'volume_target' df.columns = columns # Save the modified DataFrame back to CSV df.to_csv(file_path, index=False) # index=False to prevent adding an unnamed index columnerror: Traceback (most recent call last): File "C:\Users\Fazel\PycharmProjects\indicators_calculate\01_rename_columns.py", line 15, in for filename in os.listdir(folder_path): FileNotFoundError: [WinError 3] The system cannot find the path specified: '..\downloaded_files\Binance\test\..\downloaded_files\Binance\test\New folder'
- Assistant
The issue in your code stems from how you're constructing the
folder_path. When you're iterating over your directories with from , the object is already a object referring to each directory. However, you're trying to join it again with the , effectively duplicating the path. This leads to the system being unable to locate the constructed path, hence the .