A story that went missing when I specified a path starting with a tilde (~) in python open

path = '~/.config/remind_task/tasks.yml'
dir_name = os.path.dirname(path)
os.makedirs(dir_name, exist_ok=True) #Dig if the upper directory does not exist
with open(path, mode="w") as f:
    f.write("hoge")

If you open the file and create it like this, you will think that the file will be created in .config/remind_task/tasks.yml in your home directory. Executing the above code will create the file. But not.

> cat ~/.config/remind_task/tasks.yml
cat: /Users/atu/.config/remind_task/tasks.yml: No such file or directory

But the file has been created. As a result of searching around where it was, it was created in the current directory. In this case it was in /Users/atu/Documents/python/remind_task/~/.config/remind_task/tasks.yml.

> cat "/Users/atu/Documents/python/remind_task/~/.config/remind_task/tasks.yml"
hoge

If you want to handle a path that starts with a tilde, you can do as follows.

import pathlib
path = pathlib.Path("~/.config/remind_task/tasks.yml").expanduser()
print("path", path)
## path /Users/atu/.config/remind_task/tasks.yml

Recommended Posts

A story that went missing when I specified a path starting with a tilde (~) in python open
A story that didn't work when I tried to log in with the Python requests module
A story that turned light blue in 4 months after starting AtCoder with python
A story that I was addicted to when I made SFTP communication with python
I got stuck when trying to specify a relative path with relative_to () in python
A story that stumbled when I made a chatbot with Transformer
A reminder of what I got stuck when starting Atcoder with python
A memo that I wrote a quicksort in Python
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I made a familiar function that can be used in statistics with Python
I want to work with a robot in python.
What's in that variable (when running a Python script)
A memo that I touched the Datastore with python
A story that was convenient when I tried using the python ip address module
A template that I often use when making Discord BOT in Python (memorial note)
A memo when checking whether the specified key exists in the defined dictionary with python
I made a simple typing game with tkinter in Python
A story about how to specify a relative path in python.
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
Let's create a script that registers with Ideone.com in Python.
A story that stumbled when using pip in a proxy environment
Use a macro that runs when saving python with vscode
I tried "a program that removes duplicate statements in Python"
I made a program to collect images in tweets that I liked on twitter with Python
I made a puzzle game (like) with Tkinter in Python
The story I was addicted to when I specified nil as a function argument in Go
A story that I wanted to do a function like before_action used in rails with django [Beginner learns python with a reference book]
A story that I did not know how to load a mixin when making a front with the django app [Beginners learn python with a reference book in one hand]
Here's a summary of things that might be useful when dealing with complex numbers in Python
The story of creating a bot that displays active members in a specific channel of slack with python
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
I made a package that can compare morphological analyzers with Python
I want to use a wildcard that I want to shell with Python remove
A story that I fixed when I got Lambda logs from Cloudwatch Logs
I got an AttributeError when mocking the open method in python
[Python] A memo that I tried to get started with asyncio
I made a shuffle that can be reset (reverted) with Python
Read a file in Python with a relative path from the program
I get a can't set attribute when using @property in python
Get a list of files in a folder with python without a path
The story of making a module that skips mail with python
I made a daemon with Python
When writing a program in Python
Parsing PDFs, including text, is easy with Python ... I had a time when I was thinking that way.
(Matplotlib) I want to draw a graph with a size specified in pixels
A story I was addicted to when inserting from Python to a PostgreSQL table
I tried to develop a Formatter that outputs Python logs in JSON
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
[Python] I made a utility that can access dict type like a path
I made a tool that makes decompression a little easier with CLI (Python3)
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a module PyNanaco that can charge nanaco credit with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
[Python] A program that creates stairs with #
I made a payroll program in Python!
I made a character counter with Python
I drew a heatmap with seaborn [Python]
I tried a functional language with Python
What I did with a Python array
I made a Hex map with Python
A typed world that begins with Python