'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
'..\inputs\transactions_all\transactions_finaldf_2021-05-15_12h19m_repo.xlsx'
How can I modify my codes to get above results?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |