misguided
misguided

Reputation: 3789

Accesing UNIX variable path in Python

I want to access a file present in a certain location in UNIX box. Usually I use

Path

$HOME/my/path/filename

I am pusuing the following python code

#!/usr/bin/python
import time
mytime= time.strftime("%Y%m%d", localtime)
file = $HOME/my/path/Text.$mytime*

I am getting the following error

 file = $HOME/my/path/Text.$mytime*
               ^
SyntaxError: invalid syntax

PS: Filename is of the format

Text.YYYYMMDDhhmmss

Python Version 2.6

Upvotes: 0

Views: 121

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113940

import os
os.environ["HOME"]
os.environ["mytime"]

os.environ will provide access to your environmental variables

file = "%s/my/path/Text%s*"%(os.environ["HOME"], os.environ["mytime"])

Upvotes: 2

Related Questions