作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
# Create the new file inside of the new directory
# Return the list of files in the new directory
print(new_directory("PythonPrograms", "script.py"))
最佳答案
下面的代码是不言自明的
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
if os.path.isdir(directory) == False:
os.mkdir(directory)
# Create the new file inside of the new directory
os.chdir(directory)
with open (filename, "w") as file:
pass
os.chdir("..")
# Return the list of files in the new directory
return os.listdir(directory)
print(new_directory("PythonPrograms", "script.py"))
关于python - 检查文件是否存在,如果不存在则创建新目录并在目录内创建文件并返回目录内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60910058/
我是一名优秀的程序员,十分优秀!