Python
Windows
string literal
file path
programming tips

How should I write a Windows path in a Python string literal?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Windows paths often confuse Python beginners because Windows uses backslashes and Python uses backslashes for escape sequences inside string literals. If you write a path carelessly, pieces of it may turn into newline or tab characters instead of literal directory separators.

Why a Normal String Can Go Wrong

In Python, sequences such as \n and \t have special meaning inside ordinary string literals. That becomes a problem with paths like C:\new\temp, because Python reads \n as newline and \t as tab.

python
bad_path = "C:\new\temp\data.txt"
print(bad_path)

The printed result is not the literal path you intended. This is why file paths that look correct in source code can still fail at runtime.

Three Safe Ways to Write a Windows Path

There are three common ways to express the same path safely in Python:

python
1path1 = "C:\\new\\temp\\data.txt"
2path2 = r"C:\new\temp\data.txt"
3path3 = "C:/new/temp/data.txt"
4
5print(path1)
6print(path2)
7print(path3)

Each option is valid:

  • Double backslashes escape the backslash itself.
  • A raw string keeps backslashes literal.
  • Forward slashes also work with Python file APIs on Windows.

For quick one-off literals, raw strings are often the easiest to read. For cross-platform code, forward slashes or pathlib usually age better.

Prefer pathlib for New Code

If you are doing more than writing one literal path, pathlib.Path is the modern answer. It lets you build paths from components, avoids manual separator handling, and keeps code cleaner.

python
1from pathlib import Path
2
3base = Path("C:/Users/mark/Documents")
4report = base / "reports" / "sales.csv"
5
6print(report)
7print(report.name)
8print(report.parent)

The / operator joins path components in a readable way. On Windows, the resulting path uses Windows semantics. On other platforms, Path adapts to the current operating system.

This is especially helpful when code moves between development machines, CI systems, and production servers. Hardcoded separators become less of a problem when the path object handles joining for you.

Raw String Edge Cases

Raw strings are convenient, but they have one important limitation: they cannot end with a single trailing backslash, because that backslash would escape the closing quote.

This is invalid:

python
path = r"C:\temp\"

Instead, either remove the trailing separator or build it another way:

python
path = r"C:\temp" + "\\"
print(path)

That edge case surprises people because raw strings are often described as "backslashes stay literal." That is mostly true, but the closing quote still has to be parsed.

Use Path Construction Instead of Concatenation

Another common mistake is building paths with manual string concatenation:

python
folder = r"C:\temp"
file_name = "output.txt"
path = folder + "\\" + file_name

That works, but it scales poorly once you have many components, optional directories, or platform differences. pathlib or os.path.join makes the intention clearer.

python
1from pathlib import Path
2
3folder = Path("C:/temp")
4path = folder / "output.txt"
5print(path)

The joined form is easier to maintain and reduces small separator mistakes.

Reading and Writing with the Path

Once you have a valid path, you can use it directly with file operations:

python
1from pathlib import Path
2
3path = Path("C:/temp/example.txt")
4path.write_text("hello\n", encoding="utf-8")
5
6content = path.read_text(encoding="utf-8")
7print(content)

Most standard library functions also accept Path objects, so you do not need to convert them back to plain strings in typical code.

Common Pitfalls

  • Writing "C:\new\temp" and forgetting that \n and \t are escape sequences.
  • Using a raw string with a trailing backslash, which is invalid syntax.
  • Concatenating path fragments manually instead of using pathlib or os.path.join.
  • Hardcoding Windows-only paths in code that may later run on another platform.
  • Assuming forward slashes are invalid on Windows when Python file APIs generally accept them.

Summary

  • A normal Python string can misread Windows backslashes as escape sequences.
  • Safe options are doubled backslashes, raw strings, or forward slashes.
  • 'pathlib.Path is the best general approach for new Python code.'
  • Raw strings are convenient, but they cannot end with a single backslash.
  • Build paths from components instead of stitching separators together by hand.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.