Python Forum
Dynamic File Name to a shared folder with open command in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic File Name to a shared folder with open command in python
#9
(Jan-06-2022, 11:28 PM)bowlofred Wrote: And I have to turn the generator into a list to get the length
We need len_consume :-D

import pathlib


# this works
unc1 = pathlib.PureWindowsPath(r"\\host\share\file1")

# this seems to work also
unc2 = pathlib.PureWindowsPath(r"\\host\share") / "file1"

# the pathlib has support for unc paths, but only if the full path to the share is used
# concatenating the host part with share part will not work.

unc3 = pathlib.PureWindowsPath(r"\\host") / "share/dir/file"


print("OK", unc1)
print("OK", unc2)
print("FAIL", unc3)
Output:
OK \\host\share\file1 OK \\host\share\file1 FAIL \host\share\dir\file
I used PureWindowsPath because I'm on a Linux system.

For my understanding, it is important, that the str itself has the right format.

This is ok:
print(r"\\host\share\file1")
Output:
\\host\share\file1
This is not ok:
print("\\host\share\file1")
Output:
\host\share ile1
The first \ escapes the second backslash, if a raw string literal is not used.
The \f is the control character Form Feed.

Working with backslashes in string literals without the r-prefix, will cause problems.

The second important thing for UNC paths is, that they must supply in following structure: \\host\share.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Dynamic File Name to a shared folder with open command in python - by DeaD_EyE - Jan-07-2022, 01:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,367 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,423 Apr-24-2024, 05:47 AM
Last Post: Bronjer
Question How to add Python folder in Windows Registry ? Touktouk 1 398 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Open/save file on Android frohr 0 434 Jan-24-2024, 06:28 PM
Last Post: frohr
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 729 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  file open "file not found error" shanoger 8 1,480 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Reading a file name fron a folder on my desktop Fiona 4 1,092 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How can i combine these two functions so i only open the file once? cubangt 4 1,052 Aug-14-2023, 05:04 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 1,089 Jun-26-2023, 09:40 AM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,931 Feb-11-2023, 02:38 AM
Last Post: ted

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020