python - 使用读取的txt文件制作目录
问题描述
我正在尝试使用读取文件创建目录,但它不起作用。
x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
path=r'C:\Users\Fast Computer\Desktop'
n=f.readline()
path=os.path.join(path,n)
os.mkdir(path)
解决方案
我不知道你的文件包含什么,但是..
在你的例子中..
第 2 行已经在读取文件中的行。
第 4 行将失败,因为您正在尝试从字符串运行 readline() 命令。
从文件中读取的行也包含换行符,所以你应该去掉它们。
例子:
x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
path=r'C:\Users\Fast Computer\Desktop'
n=f.strip()
path=os.path.join(path,n)
os.mkdir(path)
推荐阅读
- android - 为什么在使用改造时我的班级中的初始化块没有被触发?
- javascript - 为图表js创建动态标签时出现问题
- mysql - 如何对齐存储为 unicode 的内容
- c# - GoogleSpeechAPI (C#) 自动检测语言
- python - 输出重定向/管道上的python失败
- python - xml to dict 在python中更改标签名称
- c++ - C++:如何制作 std::unique_ptr
指向一个新的整数? - c# - 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string”错误
- angular - 如何仅在完成 Angular 5 中的线程执行后调用函数?
- android - 检测活动启动