# Implement os.PathLike for NamedTemporaryFile

**URL:** <https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200>\
**Category:** Python Help\
**Created:** [July 2, 2024, 3:45pm UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200 "2024-07-02T15:45:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonGrace2282](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jasongrace2282/32/20850_2.png) [@JasonGrace2282](https://discuss.python.org/u/JasonGrace2282)\
**Post date:** [July 2, 2024, 3:45pm UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/1 "2024-07-02T15:45:42Z")

</div>

[`tempfile.NamedTemporaryFile`](https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile) should implement the [`os.PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) Protocol.  
This should be fairly easy to implement, as the path as a string is already avaliable as the `name` attribute.

---

<div class="post-metadata">

**Author:** ![litlighilit](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/litlighilit/32/20849_2.png) [@litlighilit](https://discuss.python.org/u/litlighilit)\
**Post date:** [July 2, 2024, 4:46pm UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/2 "2024-07-02T16:46:24Z")

</div>

It returns a [file-like object](https://docs.python.org/3/glossary.html#term-file-like-object).

Why it should implement the [`os.PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) protocol?

If it does, then it sounds like result of `open` should be of PathLike, too.

Then, the following code would be accepted by Python:

```Python
f = open('xx', 'w')
f = open(f, 'w')

```

So does for many other types.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 2, 2024, 6:06pm UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/3 "2024-07-02T18:06:47Z")

</div>

> [@JasonGrace2282](#):
>
> [`tempfile.NamedTemporaryFile`](https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile) should implement the [`os.PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) Protocol.

I think there may be a miscommunication here. `NamedTemporaryFile` gives you a file-like object. `os.PathLike` is an interface for _names of_ files (well, paths to them). When you create a `NamedTemporaryFile`, you don’t specify a name; Python figures out a suitable name (that won’t conflict with existing files or folders). And you get back the actual file handle, which you can use to `.read` or `.write` or whatever else; it doesn’t make sense for that to be `os.PathLike` - you’ve _already done the file opening_.

If you mean that the `.name` of the created file should implement the protocol, it does - it’s a string. If you mean that it should specifically be a `pathlib.Path` instance, that could break other things that don’t know about `os.PathLike`.

If you mean that e.g. the `dir` parameter for `NamedTemporaryFile` should accept anything that’s `os.PathLike` - doesn’t it already? I haven’t checked…

If you mean something else, you’re going to have to explain it more clearly. Show an example of code you think you should be able to write, explain what happens currently when you try it, explain what you think should happen instead, and justify it.

---

<div class="post-metadata">

**Author:** ![JasonGrace2282](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jasongrace2282/32/20850_2.png) [@JasonGrace2282](https://discuss.python.org/u/JasonGrace2282)\
**Post date:** [July 2, 2024, 6:26pm UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/4 "2024-07-02T18:26:47Z")

</div>

I think you guys are right, thanks for pointing out my misunderstanding 🙂

---

<div class="post-metadata">

**Author:** ![barneygale](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/barneygale/32/1882_2.png) [@barneygale](https://discuss.python.org/u/barneygale)\
**Post date:** [July 3, 2024, 8:06am UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/5 "2024-07-03T08:06:53Z")

</div>

There’s an open issue and PR for this:

> <https://github.com/python/cpython/issues/87646>
>
> BPO | \[43480\](https://bugs.python.org/issue43480)
> \--- | :---
> Nosy | @pxeger
> 
> \<su…p\>\*Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.\*\</sup\>
> 
> \<details\>\<summary\>Show more details\</summary\>\<p\>
> 
> GitHub fields:
> \`\`\`python
> assignee = None
> closed\_at = None
> created\_at = \<Date 2021-03-12.15:49:24.927\>
> labels = \['type-feature', 'library', '3.10'\]
> title = 'Add .path method/property to tempfile.\* for a pathlib.Path'
> updated\_at = \<Date 2021-03-12.15:49:24.927\>
> user = 'https://github.com/pxeger'
> \`\`\`
> 
> bugs.python.org fields:
> \`\`\`python
> activity = \<Date 2021-03-12.15:49:24.927\>
> actor = 'pxeger'
> assignee = 'none'
> closed = False
> closed\_date = None
> closer = None
> components = \['Library (Lib)'\]
> creation = \<Date 2021-03-12.15:49:24.927\>
> creator = 'pxeger'
> dependencies = \[\]
> files = \[\]
> hgrepos = \[\]
> issue\_num = 43480
> keywords = \[\]
> message\_count = 1.0
> messages = \['388540'\]
> nosy\_count = 1.0
> nosy\_names = \['pxeger'\]
> pr\_nums = \[\]
> priority = 'normal'
> resolution = None
> stage = None
> status = 'open'
> superseder = None
> type = 'enhancement'
> url = 'https://bugs.python.org/issue43480'
> versions = \['Python 3.10'\]
> \`\`\`
> 
> \</p\>\</details\>
> 
> 
> 
> \### Linked PRs
> \* gh-104466
> \* gh-114765

> <https://github.com/python/cpython/pull/114765>
>
> Add \`\_\_fspath\_\_()\` methods to \`tempfile.NamedTemporaryFile\` and \`TemporaryDirect…ory\`.
> 
> 
> 
> \* Issue: gh-87646
> 
> 
> 
> 
> \----
> 📚 Documentation preview 📚: https://cpython-previews--114765.org.readthedocs.build/

---

<div class="post-metadata">

**Author:** ![srittau](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/srittau/32/237_2.png) [@srittau](https://discuss.python.org/u/srittau)\
**Post date:** [July 3, 2024, 10:10am UTC](https://discuss.python.org/t/implement-os-pathlike-for-namedtemporaryfile/57200/6 "2024-07-03T10:10:09Z")

</div>

I still think that `NamedTemporaryFile` supporting the `PathLike` protocol makes sense. The filename is the distinguishing feature of `NamedTemporaryFile`, so it will be used. The following code would make total sense to me:

```python
with NamedTemporaryFile() as tmp:
    shutil.copyfile(tmp, "/foo/bar")

```
