開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング (Al Sweigart(著)、相川 愛三(翻訳)、オライリージャパン)の第Ⅱ部(処理の自動化)、9章(ファイルの管理)、9.7(演習プロジェクト)、9.7.1(選択コピー)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 import os import shutil try: os.mkdir('images') os.mkdir('pdfs') except Exception as err: print(err) else: for root, directories, files in os.walk('.'): for file in files: if file.endswith('.jpg'): shutil.move(os.path.join(root, file), os.path.join(os.curdir, 'images', file)) elif file.endswith('.pdf'): shutil.move(os.path.join(root, file), os.path.join(os.curdir, 'pdfs', file))
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ls -R ./ folder1/ folder3/ folder5/ sample1.py* ../ folder2/ folder4/ image6.jpg sample1.py~* ./folder1: ./ ../ image1.jpg image2.jpg pdf1.pdf ./folder2: ./ ../ folder6/ image3.jpg image4.jpg ./folder2/folder6: ./ ../ image5.jpg pdf2.pdf ./folder3: ./ ../ pdf3.pdf text1.txt ./folder4: ./ ../ text1.txt ./folder5: ./ ../ $ ./sample1.py $ ls -R ./ folder2/ folder5/ sample1.py* ../ folder3/ images/ sample1.py~* folder1/ folder4/ pdfs/ ./folder1: ./ ../ ./folder2: ./ ../ folder6/ ./folder2/folder6: ./ ../ ./folder3: ./ ../ text1.txt ./folder4: ./ ../ text1.txt ./folder5: ./ ../ ./images: ./ image1.jpg image3.jpg image5.jpg ../ image2.jpg image4.jpg image6.jpg ./pdfs: ./ ../ pdf1.pdf pdf2.pdf pdf3.pdf $
0 コメント:
コメントを投稿