怎么创建python脚本文件夹

创建文件夹

import os
 
def mkdir(path):
 
	folder = os.path.exists(path)
 
	if not folder:                   #判断是否存在文件夹如果不存在则创建为文件夹
		os.makedirs(path)            #makedirs 创建文件时如果路径不存在会创建这个路径
		print "---  new folder...  ---"
		print "---  OK  ---"
 
	else:
		print "---  There is this folder!  ---"
		
file = "G:xxoo	est"
mkdir(file)             #调用函数

推荐学习《Python教程》

os.getcwd()可以查看py文件所在路径;

在os.getcwd()后边 加上 [:-4] + 'xxoo' 就可以在py文件所在路径下创建 xxoo文件夹

import os
 
folder = os.getcwd()[:-4] + 'new_folder	est'
#获取此py文件路径,在此路径选创建在new_folder文件夹中的test文件夹
 
if not os.path.exists(folder):
    os.makedirs(folder)

创建txt文件

在桌面创建一个名字为 new 的txt文件

import os
file = open('C:UsersAdministratorDesktop' + 'new' + '.txt','w')
file.close()

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 怎么创建python脚本文件夹