在python中更改目录,相对路径

BoJack骑士

我阅读了一些帖子和文档,您可以使用更改为python中的相对路径os.path.expanduser(~/.PATHNAME)我目前正在努力使用它。当我使用它时,我最终到达目的地路径上方的一个目录。

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))
Ratatoskr

好像您缺少一步,并且您打算进入目录,如下所示:

os.chdir(os.path.expanduser('~/.usernames'))

否则,您的os.path.expanduser行只会生成一条没有任何用处的路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章