Rename filename on windows enviroment github workflow

Viewed 48

I was trying to process some data in a GitHub action.

However, due to a Japanese file name, I can not read the file successfully by:

pd.read_csv('C:\\202204_10エリア計.csv')

And I was trying to rename it before read it by:

for filename in os.listdir(download_path):
  if filename.startswith('202204'):
    filename = filename.encode('utf-8').decode(locale.getpreferredencoding(False))
    print(filename) # this print '202204_10エリア計.csv' on github action
    os.rename(os.path.join(download_path, filename), os.path.join(download_path, '202204.csv')

But it gets error:

[WinError 2] The system cannot find the file specified: 'C:\\202204_10エリア計.csv' -> 'C:\\202204.csv'
1 Answers

You should be able to sidestep the encoding/decoding entirely with pathlib:

from pathlib import Path

fn = next(p for p in Path(download_path).glob("*.csv") if p.name.startswith("202204"))
fn.rename(fn.with_stem("202204"))

This is a bit of a workaround to whatever the real issue is, however.

That said I have never needed to meddle with the encoding when using os.path, and a quick search of the docs doesn't turn up anything, so you may be fine if you simple remove your encoding/decoding step. I would expect the os.path api to use the same internal representation throughout.

Related