即使目录不存在,os.makedirs也会抛出异常

时间:2018-03-31 10:11:17

标签: python sockets ftp mkdir

我正在使用套接字为项目编写python中的FTP服务器。我无法实现创建新目录的命令。

我的代码如下:self.dir是新目录的名称,newDirectory = os.path.join(self.dir, path) try: os.mkdir(newDirectory) except Exception as e: print(e) return 是当前的工作目录。

[WinError 183] Cannot create a file when that file already exists

我收到此错误:

game

无论输入什么路径。此外,它会创建文件,但仍会抛出该异常。

1 个答案:

答案 0 :(得分:1)

尝试这种方法

newDirectory = os.path.join(self.dir, path)

if not os.path.exists(newDirectory):
    os.makedirs(newDirectory)

发生错误是因为它没有创建导致最后一个主文件夹的所有子目录。

相关问题