简体   繁体   English

open('.....", 'w+') 不创建新文件

[英]open('....", 'w+') does not create a new file

I'm using:我在用着:

import os
path = os.chdir("/my/path")

Websites = ["list", "of", "websites"]


#print(os.getcwd()). -gives the correct result
#print(os.listdir()) -shows my other files, both are correct

def CreateFiles(Websites):
    for W in Websites:
        f = open(W + '.txt','w+') #w+ should open it because it doesn't exist right?
        f.write(W)
        f.close()

CreateFiles(Websites)

but i get the error No such file or directory: 'list.txt'但我收到错误No such file or directory: 'list.txt'

Am i missing something?我错过了什么吗? every example i see uses open(name, read or write argument) .我看到的每个例子都使用open(name, read or write argument) I'm using python 3 on mac.我在 mac 上使用 python 3。

Answer code:答案代码:

import os
path = os.chdir("/my/path")

Websites = ["https://www.website.com/","https://www.Anotherwebsite.com/"]


#print(os.getcwd()). -gives the correct result
#print(os.listdir()) -shows my other files, both are correct

def CreateFiles(Websites):
    for W in Websites:
        P = W.split('.')[1]
        f = open(P + '.txt','w+')
        f.write(P)
        f.close()

CreateFiles(Websites)

this creates files of website.txt and Anotherwebsite.txt这会创建 website.txt 和 Anotherwebsite.txt 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM