我在一些帖子和文档中读到,您可以使用 os.path.expanduser(~/.PATHNAME) 更改为 python 中的相对路径。我现在很难使用它。当我使用它时,我最终会到达目标路径上方的一个目录。
from django.shortcuts import render
import os
import subprocess
def index(request):
os.path.expanduser('~/.usernames')
files = []
for file in os.listdir("."):
files.append(file)
return render(request, 'sslcert/index.html', dict(files = files))
最佳答案
看起来您缺少一个步骤,并且您打算进入该目录,如下所示:
os.chdir(os.path.expanduser('~/.usernames'))
否则你的 os.path.expanduser 行只是生成一个无用的路径。
关于python - 更改python中的目录,相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19862427/