0

I need to build a Windows (only) file path from a combination of a UNC path passed to my script as a parameter, an API call, and a calculated date.

I'm having a horrible time of it, mostly caused by Windows' use of a backslash character to delimit file paths. I've read that the "pathlib" module should be able to sort this mess out, BUT it apparently doesn't support concatenation when building out file paths.

The UNC path is being passed out to the script as a dictionary from another application (PRTG Network Monitor:

{"fileshare": "//server02/logs/"}

I read that in and then need to append a hostname derived from an API call:

logPath = Path(params["fileshare"] + "/" + apiHostname + "/")

I then calculate a date which needs to be appended to the logpath, along with a delimiter "-" and a filename suffix:

filePath = Path(logPath, + apiHostname + "-", + past_day + ".log" )

The problem arises during the concatenation:

{"text": "Python Script execution error: unsupported operand type(s) for +: 'WindowsPath' and 'str'", "error": 1}}

Can someone please explain how I can build a path so that the calculated filename, which should look like this:

\\server02\logs\log01.rhmgmt.lan\log01.rhmgmt.lan-2021-07-28.log

Can be opened for processing?

martineau
  • 119,623
  • 25
  • 170
  • 301
Brian
  • 37
  • 5
  • `Path(params["fileshare"] + "/" + apiHostname + "/")` looks wrong. You usually call these functions with comma-separated arguments and it does the concatenation for you. Usually I use `os.path.join(foo, bar, baz)`. `logPath, + apiHostname` is just a typo -- there's a hanging `+` in the middle of the argument list -- remove that. – ggorlen Jul 29 '21 at 20:22
  • Can't you use the [`joinpath()`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.joinpath) method to concatenate them? – martineau Jul 29 '21 at 20:27
  • If you are using `pathlib.Path` then you should be using its functionality - the `/` operator - to join up paths, not string concatenation. Also note that *forward slashes work just fine for Windows paths in normal circumstances*; `cmd.exe` accepts them (they just mess with tab completion etc.) and Python programs accept them. [C++ programs accept them, too,](https://stackoverflow.com/questions/29119638/why-path-in-vs-c-contains-forward-slash-not-backslash) and it seems to be implemented on the Windows side. – Karl Knechtel Jul 29 '21 at 20:48

2 Answers2

0

Yes "pathlib" module should be able to sort this mess out

Input:

from datetime import date, timedelta
from pathlib import Path

params = {"fileshare": "//server02/logs/"}
apiHostname = 'log01.rhmgmt.lan'
past_day = str((date.today() - timedelta(days=1)))

Create the initial path and append all parts:

fileshare = Path(params['fileshare'])
filepath = fileshare / apiHostname / f"{apiHostname}-{past_day}.log"

Output:

>>> filepath
PosixPath('//server02/logs/log01.rhmgmt.lan/log01.rhmgmt.lan-2021-07-28.log')
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Yes, pathlib can easily sort things out. You can use the joinpath() method, as I suggested in a comment, to concatenate the components are you're building the Path. It does the equivalent of what os.path.join() does.

The only slightly tricky part is you'll have to initially create an empty Path in order use the method they inherit from PurePath.

from datetime import date, timedelta
from pathlib import Path

params = {"fileshare": "//server02/logs/"}
apiHostname = 'log01.rhmgmt.lan'
past_day = str((date.today() - timedelta(days=1)))

filePath = Path().joinpath(params["fileshare"], apiHostname, 
                           apiHostname + '-' + past_day + '.log')
print(filePath)  # -> \\server02\logs\log01.rhmgmt.lan\log01.rhmgmt.lan-2021-07-29.log

martineau
  • 119,623
  • 25
  • 170
  • 301