8
3

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 2018-06-27

概要

私はよく読書のレポートを書きます。そこで、そのレポートの中から毎日1ファイル読み返したいなと思い、その選択をPythonにやってもらうことにしました。自分で選択すると、好みや気分で偏りができる懸念があったからです。
ちなみに、結果は自分専用のSlackにrequestsを使って投げてもらうようにしていますが、今回はランダム選択の部分だけを書きます。

環境

windows10
Python3.6.5
Anaconda3

実装

必要なインポート

Choice_report.py
import glob
import random

ファイルのランダム選択

Choice_report.py
def choice_report():
    reportfiles = [r.split('/')[-1] for r in glob.glob('探索するフォルダのパス')]
    posttext = '本日読み返すレポートは' + random.choice(reportfiles) + 'です。'
    return posttext

ワイルドカード

ワイルドカードを使い、拡張子なんかを指定してあげることも可能です。
以下はPDFファイルを抽出する例です。

../Documents/PDF/*.pdf

あとは出来たテキストをLINEなりSlackなりに投げてあげるようにすれば勝手が良いです。タスクスケジューラなんかに入れて、自動化するのも良いと思います。

まとめ

読書と勉強が捗る気がします。

8
3
4

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
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?