1. 程式人生 > >Python3基礎 os mkdir 創建一層文件夾 在有父目錄的情況下創建子目錄

Python3基礎 os mkdir 創建一層文件夾 在有父目錄的情況下創建子目錄

res help file home down rules ice str 有一種

?

  • ???? Python : 3.7.0
  • ?????? OS : Ubuntu 18.04.1 LTS
  • ?????? IDE : PyCharm 2018.2.4
  • ????? Conda : 4.5.11
  • ???typesetting : Markdown

?

example_1

code

coder@Ubuntu:~$ source activate py37
(py37) coder@Ubuntu:~$ ipython
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type ‘?‘ for help.

In [1]: import os

In [2]: os.mkdir(‘/home/coder/not_exist_dir_father/not_exist_dir_son‘)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-dfab67f068d5> in <module>()
----> 1 os.mkdir(‘/home/coder/not_exist_dir_father/not_exist_dir_son‘)

FileNotFoundError: [Errno 2] No such file or directory: ‘/home/coder/not_exist_dir_father/not_exist_dir_son‘

In [3]: # 在父目錄不存在的情況下,會拋異常的

In [4]: exit
(py37) coder@Ubuntu:~$ source deactivate
coder@Ubuntu:~$ 

?

example_2

code

coder@Ubuntu:~$ source activate py37
(py37) coder@Ubuntu:~$ ipython
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type ‘?‘ for help.

In [1]: import os

In [2]: # 一級一級的創建文件夾

In [3]: os.mkdir(‘/home/coder/not_exist_dir_father‘)

In [4]: os.mkdir(‘/home/coder/not_exist_dir_father/not_exist_dir_son‘)

In [5]: cd /home/coder/
/home/coder

In [6]: tree not_exist_dir_father  # 使用tree命令查看文件夾結構
  File "<ipython-input-6-796e24880147>", line 1
    tree not_exist_dir_father  # 使用tree命令查看文件夾結構
                            ^
SyntaxError: invalid syntax


In [7]: exit
(py37) coder@Ubuntu:~$ source deactivate
coder@Ubuntu:~$ 

在終端中使用tree命令查看文件夾結構

coder@Ubuntu:~$ ls -l
總用量 88
drwxr-xr-x 23 coder coder  4096 9月  30 18:41 anaconda3
drwxr-xr-x  4 coder coder  4096 10月  2 11:57 Desktop
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Documents
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Downloads
-rw-r--r--  1 coder coder  8980 9月  30 17:26 examples.desktop
-rwxrwxr-x  1 coder coder  2354 4月  28  2009 get-fonts.sh
-rw-rw-r--  1 coder coder  1042 4月  28  2009 get-fonts.sh.zip
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Music
drwxr-xr-x  3 coder coder  4096 10月  2 14:22 not_exist_dir_father
drwxr-xr-x  3 coder coder 12288 10月  1 17:38 Pictures
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Public
drwxr-xr-x 11 coder coder  4096 9月  30 18:51 pycharm-2018.2.4
drwxrwxr-x  5 coder coder  4096 10月  2 12:04 PycharmProjects
drwxr-xr-x  3 coder coder  4096 9月  30 22:09 snap
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Templates
drwxr-xr-x  2 coder coder  4096 9月  30 18:51 ubuntu_software
drwxr-xr-x  2 coder coder  4096 10月  1 01:40 Videos
coder@Ubuntu:~$ tree not_exist_dir_father
not_exist_dir_father
└── not_exist_dir_son

1 directory, 0 files
coder@Ubuntu:~$ 

?

more knowledge

  • 有道是:
    不積跬步,無以至千裏;不積小流,無以成江海。騏驥一躍,不能十步;駑馬十駕,功在不舍。鍥而舍之,朽木不折;鍥而不舍,金石可鏤。

?

reference

  • [1] blog.csdn.net/qq_28602957/article/details/52511529

?

resource

  • [文檔] docs.python.org/3
  • [規範] www.python.org/dev/peps/pep-0008
  • [規範] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源碼] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平臺] www.cnblogs.com
  • [平臺] gitee.com

?


Python具有開源、跨平臺、解釋型、交互式等特性,值得學習。
Python的設計哲學:優雅,明確,簡單。提倡用一種方法,最好是只有一種方法來做一件事。
代碼的書寫要遵守規範,這樣有助於溝通和理解。
每種語言都有獨特的思想,初學者需要轉變思維、踏實踐行、堅持積累。

Python3基礎 os mkdir 創建一層文件夾 在有父目錄的情況下創建子目錄