When Python opens the wrong file on Windows, check two things. First, does the path start at a drive root, or does it still depend on a working directory? Then ask whether the file belongs beside your script or belongs to whoever launched the command. Those decisions tell you which base to use and which path to print when the file is missing.
Use Path for files on the operating system running your code. Use PureWindowsPath when you need to inspect Windows path text without opening anything. A Linux build worker can use the latter to inspect a Windows export, while a Windows script usually wants a concrete Path for reads and writes. You can try both using a single small CSV file and a script that shows its decisions.
Identify the starting point in a Windows path
A Windows path can contain a drive without starting at the root of that drive. For example, C:\Reports\weekly.csv starts at the root of drive C. In C:Reports\weekly.csv, the separator after the colon is missing. That path is relative to the current directory associated with drive C. If a colleague copies the second form into a settings file and your script runs under a different environment, the file it selects can change.

A name like \Reports\weekly.csv has a root and no drive. In a Windows path context, it refers to a location at the root of a drive, but the drive still has to come from somewhere, such as the current drive or an explicit left-hand path in a join. Reports\weekly.csv has neither a drive nor a root. It is the common case where you need to know the working directory before you can tell which file it means.
A UNC path such as \\fileserver\exports\weekly.csv has a server and share instead of a letter. Here, the server and share together are the drive portion as pathlib understands it. A complete UNC path can be absolute while the share is disconnected or while your account lacks access. Absolute describes where the path starts. Availability is a separate question you answer by attempting the operation under the relevant Windows account.
Microsoft’s Windows path-format documentation describes these differences, and Python’s pathlib reference shows how the module represents them. Inspecting both drive and root makes errors visible that a glance at the letter C would miss.
Make the two parts of the exercise reproducible
Save the script below as show_windows_paths.py. Beside it, create a directory nameddata, then create weekly.csv in that directory. Give the file a first line of sku,units and a second line of A17,4. Save it as UTF-8 without a byte order mark for this exercise. If you need help placing the script, see how to run a first Python script on Windows.
The first part examines Windows path syntax without checking any of the named locations. It is fine if there is no Reports folder or file server. The second part prints and opens a real file. Keep the filename and directory names from the instructions until the first run works. That gives you a known case to use when you try your own path.
from pathlib import Path, PureWindowsPath
import sys
samples = (
r"C:\Reports\weekly.csv",
r"C:Reports\weekly.csv",
r"\Reports\weekly.csv",
r"Reports\weekly.csv",
r"\\fileserver\exports\weekly.csv",
)
for text in samples:
path = PureWindowsPath(text)
print(f"{text!r}: drive={path.drive!r}, root={path.root!r}, "
f"absolute={path.is_absolute()}")
base = PureWindowsPath(r"C:\Projects\Demo")
print("Child:", base / "data" / "weekly.csv")
print("Rooted segment:", base / r"\data\weekly.csv")
script_folder = Path(__file__).resolve().parent
sample = script_folder / "data" / "weekly.csv"
if len(sys.argv) > 2:
raise SystemExit("Pass one file path, quoted if it contains spaces.")
candidate = Path(sys.argv[1]) if len(sys.argv) == 2 else sample
print("Working directory:", Path.cwd())
print("Input candidate:", candidate.resolve())
try:
with candidate.open(encoding="utf-8", newline="") as stream:
print("First line:", stream.readline().rstrip("\r\n"))
except FileNotFoundError:
raise SystemExit("File missing. Check the input candidate above.")
Run python .\show_windows_paths.py from the example directory. If you normally use the Windows Python launcher, use py in place of python. The output shows that the drive-qualified rooted path and the UNC path are absolute, while the other samples are not. It also shows the rooted segment starting underC:\data, even though the base folder started under C:\Projects\Demo. The last line should read First line: sku,units.
The doubled backslashes inside quoted output come fromrepr, requested by !r in the formatting expression. Python uses that form to make a string’s escape characters visible. It does not mean the filesystem gained extra separators. The first value comes from an ordinary Windows drive path with one separator between each component. The UNC example starts with two separators in the path itself, and the representation displays escapes for both.
Choose a base according to who supplied the file
A file that ships with the script
Keep the supplied sample beside the script and build its path from Path(__file__).resolve().parent. Start a shell in another folder and run the script using its absolute filename in quotes. The working directory line will change. The input candidate and first CSV line should stay the same because the sample belongs to the script’s folder.
This pattern is useful for a local template, a bundled export used in a test, or a small configuration file installed alongside a script. It assumes ordinary file-based execution where __file__ is available. At the interactive console or in many notebook contexts, there may be no script filename to use. Pass an explicit data directory in those cases. A packaged application may also need its resource-loading API instead of assuming that every resource is a normal file beside the executable.
A file the shell user picked
Supply a separate filename to the script to use the caller’s path. If you create a second weekly.csv in the shell’s working directory and run the same script with weekly.csv as an argument, Python opens the caller’s copy. Give that second file a different header, such asname,count, to prove which copy won. You can do this without moving the original script or editing its saved data.

Quote an argument containing spaces so the shell keeps it together. Those shell quotes are separate from the quotes used when you write a Python string literal. A user-supplied relative argument should usually stay relative to where the user ran the command. A script that silently attaches its own parent folder to every argument can open the wrong file even if the program appears to work.
Once you know the file being opened, read the CSV with Python’s standard library to handle headers, quoted commas, and missing values. It is easier to debug the parser once you have proved that the program selected the intended input.
Join child names without replacing the base
In a path expression, base / "data" / "weekly.csv" adds two child names. Compare that with base / r"\data\weekly.csv". The first backslash in the latter segment puts it at the drive root. The drive comes from the left side, but Python drops the folders in that base. A fully qualified path on another drive can replace the drive and folders together.

An effective editing check is to print each segment separately before a join and compare the finished path with the one you intended. If you are building a child from a filename you control, use an ordinary relative name without a leading slash. If a settings field accepts either a full path or a filename, document that distinction and resolve it by policy rather than expecting the slash operator to guess. This example takes the command-line argument as supplied because selecting another location is a feature of that argument.
For fields meant to contain an ordinary filename only, reject values with directory components and drive or root information. Also remember that string checks alone do not prove a filesystem operation stays within an approved directory. Junctions, symlinks, and directory changes can alter where an operation goes. If the program accepts untrusted input and confinement matters, handle that as a separate design problem rather than relying on a lexical join.
Read each error at the layer where it occurs
If you get a syntax error about a Unicode escape while typing a Windows path, inspect the Python literal first. r"C:\Users\Public\data.csv" Keeps the backslashes as text. "C:/Users/Public/data.csv" is another convenient ordinary drive-path literal on Windows. A raw string still cannot end in an odd number of backslashes. Build a path from components if you need a folder path with a clear join boundary.
If the script prints an input candidate you did not expect, compare it with the working directory line. A relative path often resolves differently than it does from a terminal, an editor run button, or a scheduled task. Check also for a duplicate extension, such as weekly.csv.csv if Explorer hides known extensions. When your tool opens a file selected from its own installation, print where the script’s data folder actually is and check that the folder traveled with the script.
resolve() helps you display the candidate as an absolute path and can resolve symlinks where possible. In its default non-strict mode, the displayed path can still name a file that does not exist. Attempt the open and handle the error that results rather than treating the diagnostic print as proof of access. If the named path is on a share, first verify that the account running the script can read it. Running a different editor or opening a terminal as administrator changes the context, which makes the original problem harder to isolate.
Finish by changing one input at a time. Run from the original folder, then a different folder, then point the script at a deliberately missing file. You should be able to explain each input candidate before looking at the resulting first line. Once you can do that, the CSV and the shell stop being a guessing game and you can choose the base deliberately in the next script.
Check the stored path before it reaches pathlib
When an exported setting looks correct on screen and still fails, print its decoded representation with repr. A tab hiding in the middle of a folder name is easier to spot when it is displayed as an escape. If the value came from a JSON file, confirm the escaping rules in that file with a real JSON reader. A Python raw string prefix is syntax for the Python source itself and has no meaning when it is typed into another format.
Compare the drive and root of the decoded value with the original path from Explorer. Then test any edited setting with a disposable file in a folder whose name contains spaces. Use the same shell or editor that will normally execute the script. This catches two separate mistakes: a path that points at an unexpected directory, and a shell command that has split a single filename into more than one argument.
