python创建目录包括父目录_Python3基础 os mkdir 创建一层文件夹 在有父目录的情况下创建子目录...

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)

in ()

----> 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 "", 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的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值