5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで隠しフォルダを作成する

Last updated at Posted at 2019-10-25

ありそうでなかったので書いた。

import pathlib
import subprocess

def mkdir_hidden(self):
    renamed = self.parent / f'.{self.name}'
    renamed.mkdir(parents=True, exist_ok=True)
    if type(self) == pathlib.WindowsPath:
        cmd = ['attrib', '+H', str(renamed)]
        subprocess.run(cmd, shell=True)
    return renamed

pathlib.Path.mkdir_hidden = mkdir_hidden
hoge = pathlib.Path(__file__).parent / 'hoge'
hoge = hoge.mkdir_hidden()

Linuxは名前の頭に.がついていれば隠し扱いになりますがWindowsの場合は属性を設定してあげなきゃいけないのでそんな感じにしてます。

5
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?