BoJack Horseman
BoJack Horseman

Reputation: 4452

Change directory in python, relative path

I read in some posts and documentations that you can change to relative paths in python with os.path.expanduser(~/.PATHNAME). I struggle with using it at the moment. When I use it, I end up one directory above the destined path.

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

Upvotes: 2

Views: 2675

Answers (1)

ratatoskr
ratatoskr

Reputation: 437

It looks like you're missing a step, and that you intend to go into the directory, like this:

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

Otherwise your os.path.expanduser line is just generating a path that is used for nothing.

Upvotes: 4

Related Questions