Python如何实现自动在指定文件路径下创建文件夹功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计285个文字,预计阅读时间需要2分钟。
1. 文件路径:`D:\Auto\eclipse\workspace\Test\Test`
2.当前路径:`D:\Auto\eclipse\workspace\Test`
3.截取路径:`D:\Auto\eclipse\workspace\T`
1、a.py文件存放的路径下为(D:\Auto\eclipse\workspace\Testhtml\Test)
2、通过os.getcwd()获取的路径为:D:\Auto\eclipse\workspace\Testhtml\Test
3、使用os.getcwd()[:-4]截取到的路径为:D:\Auto\eclipse\workspace\Testhtml\
4、使用下面的命令则在3步骤下新建report文件夹,并将执行的结果会自动保存在report文件夹下
import osFile_Path = os.getcwd()[:-4] +'report\\' #获取到当前文件的目录,并检查是否有report文件夹,如果不存在则自动新建report文件
if not os.path.exists(File_Path):
os.makedirs(File_Path)
1.作者:Syw
2.本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
3.如果文中有什么错误,欢迎指出。以免更多的人被误导。
本文共计285个文字,预计阅读时间需要2分钟。
1. 文件路径:`D:\Auto\eclipse\workspace\Test\Test`
2.当前路径:`D:\Auto\eclipse\workspace\Test`
3.截取路径:`D:\Auto\eclipse\workspace\T`
1、a.py文件存放的路径下为(D:\Auto\eclipse\workspace\Testhtml\Test)
2、通过os.getcwd()获取的路径为:D:\Auto\eclipse\workspace\Testhtml\Test
3、使用os.getcwd()[:-4]截取到的路径为:D:\Auto\eclipse\workspace\Testhtml\
4、使用下面的命令则在3步骤下新建report文件夹,并将执行的结果会自动保存在report文件夹下
import osFile_Path = os.getcwd()[:-4] +'report\\' #获取到当前文件的目录,并检查是否有report文件夹,如果不存在则自动新建report文件
if not os.path.exists(File_Path):
os.makedirs(File_Path)
1.作者:Syw
2.本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
3.如果文中有什么错误,欢迎指出。以免更多的人被误导。

