Home > Net >  Invalid argument error when using f string in path of DataFrame.to_csv()
Invalid argument error when using f string in path of DataFrame.to_csv()

Time:01-03

I want to write pandas dataframe to a csv file every 10 secs. The csv file name includes the current timestamp. Here is part of the code:

import pandas as pd
import time

while True:
    df = pd.read_sql_query('select * from dbo.tbl_tag_values', cnxn)
    t = time.localtime()
    current_time = time.strftime('%Y-%m-%dT%H:%M:%S',t)
    csv_path =f'C:/Users/00_Projects/App/data-{current_time}.csv'
    df.to_csv(csv_path)
    time.sleep(10)

Without using f-string and a static file name, the script works fine but with the f-string I get the error:

Traceback (most recent call last):
  File "c:\Users\00_Projects\App\script.py", line 24, in <module>
    df.to_csv(csv_path)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3466, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\format.py", line 1105, in to_csv
    csv_formatter.save()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\csvs.py", line 237, in save
    with get_handle(
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\common.py", line 702, in get_handle
    handle = open(
OSError: [Errno 22] Invalid argument: 'C:/Users/00_Projects/App/data-2023-01-02T15:33:19.csv'

I read this post How to use f string in a path location and tried Path from pathlib but got the same error. My OS is windows.

Thanks for any help!

CodePudding user response:

The colon is not a valid character for a file name in Windows. One solution to this problem would be to replace the colon in the timestamp with another character like this:

current_time = time.strftime('%Y-%m-%dT%H:%M:%S', t).replace(':', '_')
  • Related