简体   繁体   中英

Calling open() with 'append' mode throws: [Errno 2] No such file or directory exception in Python

It is kind of weard situation and I can not find similar problem anywhere. In my situation we call 'open' with 'append' parameter, as below.

I am using a function that calls other mehod:

fileManager.saveNewLine(result_line, results_path, "a")

#below is fileManager
def saveNewLine(text, path, mode):
    hs = open(path, mode) #<-- throws: [Errno 2] No such file or directory...
    hs.write(text + "\n")
    hs.close()

And the file path is:

'ml_models/signals/results/model-training-1h-Core-hourly-True-True-True-0.2-1-9-500-32-1-0.2-1-5-lstm-relu-linear-5000-300-10-10-250-250-6000-14-13000-col--MlModel-time-2023-01-14_23-12-24.301759.txt'

The path (relative) has only 198 characters. The folder ml_models/signals/results exists. I ran out of ideas what else can be wrong.

As far as I know 'append' should create new file if exists. Tested also with w parameter.

"a" - Append - Opens a file for appending, creates the file if it does not exist.

"w" - Write - Opens a file for writing, creates the file if it does not exist

It appears, that it was an issue with path length. For path below it is fine:

'ml_models/signals/results/something-time-2023-01-14_23-12-24.301759.txt'

My conclusion is that for file system counts absolute not relative file path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM