Skip to content
Advertisement

why I got mixed file path separators?

I am using windows 10 with jupyter notebook under annaconda.

last_file = glob('../inputs/transactions_all/transactions*.xlsx')[-1] # path to file in the folder
last_file

'../inputs/transactions_all\transactions_finaldf_2021-05-15_12h19m_repo.xlsx'

from the above, I got / and \ for path separators? I am expecting the result like this

'..inputstransactions_alltransactions_finaldf_2021-05-15_12h19m_repo.xlsx'

How can I modify my codes to get above results?

Advertisement

Answer

there is no problem working with either or /, python know how to work with those (in windows at least), that being said if you want to ensure the correct one you can use normcase

>>> import os
>>> p='../inputs/transactions_all\transactions_finaldf_2021-05-15_12h19m_repo.xlsx'
>>> os.path.normcase(p)
'..\inputs\transactions_all\transactions_finaldf_2021-05-15_12h19m_repo.xlsx'
>>>     

You can also use the pathlib module to handle your paths that way they always will be the appropriate one for the OS you are working on

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement