简体   繁体   English

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

[英]how to set a path to save the file during runtime using python?

I have created a new directories 'Student' and 'Faculty' using os.mkdir(). 我使用os.mkdir()创建了一个新目录'Student'和'Faculty'。 I need save the student files in 'Student' folder and employee details in the 'Faculty' folder. 我需要将学生文件保存在“学生”文件夹中,并将员工详细信息保存在“学院”文件夹中。 How do I do it manually? 如何手动执行? I need to set a path to write a file into the student and faculty folder. 我需要设置一个路径来将文件写入到Student and 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)

Try changing the current directory to the directory you want using os.chdir('complete_path_you_want_to_switch') if else blocks and try writing the files there. 如果其他目录阻塞,请尝试使用os.chdir('complete_path_you_want_to_switch')将当前目录更改为所需的目录,然后尝试在该目录中写入文件。

os.chdir() os.chdir()

Use os.getcwd to the current working directory 使用os.getcwd到当前工作目录

path=os.getcwd();

Then append directory,outfile to that path 然后将目录,输出文件追加到该路径

f=open(path+stuDir+outfile, 'w')

Just use os.path.dirname(__file__) if you want a relative path, and use os.path.join() to join paths. 如果需要相对路径,只需使用os.path.dirname(__file__) ,然后使用os.path.join() os.path.dirname(__file__)路径。 To write the file in the folder you just created, simply use the same filepath, as in this example: 要将文件写入刚刚创建的文件夹中,只需使用相同的文件路径,如本例所示:

import os
stuDir = 'StudentDetails'
stuDir_filepath = os.path.join(os.path.dirname(__file__), stuDir)
os.mkdir(stuDir_filepath)

facDir = 'FacultyDetails'
facDir_filepath = os.path.join(os.path.dirname(__file__), facDir)
os.mkdir(facDir_filepath)

name_of_file = "name_file"
file_path= os.path.join(facDir_filepath, name_of_file+".txt")         
file1 = open(file_path, "w")
toFile = "Some Text here"
file1.write(toFile)
file1.close()

For Python 3.6+ there is a new Path object from pathlib which can work like this for your situation. 对于Python 3.6 +pathlib中有一个新的Path对象,可以根据您的情况像这样工作。 In this example there is not a need to set a path. 在此示例中,无需设置路径。 stuDir and facDir are assumed to be local paths and they can give you the full path with .absolute() afterwards eg facDir.absolute() 假定stuDir和facDir是本地路径,然后它们可以使用.absolute()为您提供完整路径,例如facDir.absolute()

from pathlib import Path
outfile = "somefilenamehere.csv"
stuDir = Path('StudentDetails')
facDir.mkdir(exist_ok=True)

facDir = Path('FacultyDetails')
facDir.mkdir(exist_ok=True)

if(tempNo[0]=='E'):
    #now outfile is a path
    outfile = facDir/outfile
elif(tempNo[0]=='R'):
    outfile = facDir/outfile
#try out this last line as I can't verify that it works without some data which was not provided.
outfile.write_lines([csv.writer(j) for j in tempList2 if temp==j[0]]

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

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