python - 如何使用python设置运行时保存文件的路径?

标签 python file directory filepath

我使用 os.mkdir() 创建了一个新目录“Student”和“Faculty”。我需要将学生文件保存在“学生”文件夹中,将员工详细信息保存在“教师”文件夹中。我该如何手动操作?我需要设置一个路径以将文件写入学生和教职员工文件夹。

import os
stuDir = 'StudentDetails'
os.mkdir(stuDir)
facDir = 'FacultyDetails'
os.mkdir(facDir)
if(tempNo[0]=='E'):
 #I need to set a path to 'Faculty'folder
elif(tempNo[0]=='R'):
 #I need to set a path to 'Student'folder
f=open(outfile, 'w')
for j in tempList2:
  if(temp==j[0]):
   writer = csv.writer(f)
   writer.writerow(j)

最佳答案

尝试使用 os.chdir('complete_path_you_want_to_switch') 将当前目录更改为您想要的目录if else 阻塞并尝试在那里写入文件。

os.chdir()

关于python - 如何使用python设置运行时保存文件的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57000595/

相关文章:

python - 我需要一个 numpy 数组中的 N 个最小(索引)值

vb.net - Win32Exception “The System Cannot Find the File Specified”

javascript - 如何在 JavaScript 中创建一个返回本地文本文件值的函数?

windows - 如何获取文件放置在 Windows 目录中的日期/时间?

python - Django错误: TypeError: __init__() got an unexpected keyword argument 'attrs'

python - 抓取无限滚动页面

python - scikit 特征重要性选择经验

java - Vector.remove() for 循环

javascript - 如何返回目录中的文件? (JavaScript、Node.js)

c# 使用目录类搜索