python3输入文件路径,若没有,则自动创建文件夹与文件名,并读写文件?
2条回答 默认 最新
关注import os fullpath = "D:\\data\\text\\Test\\test1.txt" paths, file = [], None path = fullpath[:] while file!='': path,file = os.path.split(path) if file: paths.append(file) while len(paths)>1: path += ('' if path[-1]=='\\' else '\\') + paths.pop() if not os.path.isdir(path): os.mkdir(path) with open(fullpath, 'w') as f: f.write('test:测试内容!')本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用