0

I'm trying to build a path in Python (windows) and frustratingly enough it gives me wrong path every time. The path I'm trying to build is C:\Users\abc\Downloads\directory\[log file name].

So when I use print(os.getcwd()) it returns C:\Users\abc\Downloads\directory which is fine. But when I try to use the os join in python, (os.path.join(os.path.abspath(os.getcwd()),GetServiceConfigData.getConfigData('logfilepath'))) it returns only C:\Logs\LogMain.log and not the desired output. (Path.cwd().joinpath(GetServiceConfigData.getConfigData('logfilepath'))) also returns the same result.

logfilepath is an XML string <add key="logfilepath" value="\Logs\LogMain.log" />

4
  • print(os.getcwd()) works, but os.path.join(os.path.abspath(os.getcwd()),GetServiceConfigData.getConfigData('logfilepath')) doesn't - try deleting os.path.abspath in the not-working line. And try print(os.path.abspath(os.getcwd())). Commented Feb 5, 2021 at 12:35
  • os.path.join(os.getcwd(),GetServiceConfigData.getConfigData('logfilepath')) ->C:\Logs\LogMain.log Path.cwd().joinpath(GetServiceConfigData.getConfigData('logfilepath'))-> C:\Logs\LogMain.log os.path.abspath(os.getcwd()) -> C:\Users\xxxx\Downloads\directory Commented Feb 5, 2021 at 12:43
  • Wow, that's really weird ... What does it print if you execute print(os.path.join("C:\Users\xxxx\Downloads\directory", "LogMain.log"), os.path.join(os.path.abspath("C:\Users\xxxx\Downloads\directory"), "LogMain.log") ? Commented Feb 5, 2021 at 14:33
  • first check what you get with GetServiceConfigData.getConfigData('logfilepath') - if it gives C:\Logs\LogMain.log or \Logs\LogMain.log then it is absolut path and it will not join with other folder. You would have to remobe starting \ from \Logs\LogMain.log to get relative path Logs\LogMain.log and then it will join with other folder. OR you should get only filename from \Logs\LogMain.log using ie, split('\')[-1] Commented Feb 5, 2021 at 16:07

2 Answers 2

1

Thanks for all the help, in the end it was solved by removing 1 backslash.

<add key="logfilepath" value="\Logs\LogMain.log" />

to

<add key="logfilepath" value="Logs\LogMain.log" />

Sign up to request clarification or add additional context in comments.

Comments

0

Your logfilepath is \Logs\LogMain.log and this is absolute path (without drive), not relative path, and not only filename - so it will not join as you expect.

When you try to join absolute path then it keeps only drive and replaces previous absolute path.

You have to get only filename from \Logs\LogMain.log - ie.

 logfilepath.split('\\')[-1]

to have only LogMain.log and finally get

 C:\Users\abc\Downloads\directory\LogMain.log

folder = os.path.abspath(os.getcwd())

logfilepath = GetServiceConfigData.getConfigData('logfilepath')
filename = logfilepath.split('\\')[-1]

os.path.join(folder, filename)

EDIT

If logfilepath is object pathlib.Path like

from pathlib import Path
p = Path('\Logs\LogMain.log')

then you may get only filename using

p.name

2 Comments

Your point is absolutely correct. Albeit I wanted to it to be in a separate folder like "Logs\LogMain.log"; nevertheless, problem solved by your suggestion!
if you want in separated folder then you have to remove starting \ before Logs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.