have two variables to be joined as a path using python - python

Hi i have two variables to be joined as a path,
SUITE_DIR = D:/Squish and
SUITE_NAME = HMI_Remote
Now i want DIR_name as D:/Squish/HMI_Remote
when i tried
os.path.join(SUITE_DIR,SUITE_NAME)
it gave me D:/Squish\HMI_Remote
why is it so and how get it right ?
thanks in advance
Brijesh

In os.path there is a function normpath, which gets the input straight (and resolves relative parts and some further improvements).

os.path.join uses the separator of the OS, which in the case of Windows, is \. Windows can use either \ or / though.
Just change SUITE_DIR to be SUITE_DIR = 'D:\Squish' and you'll be fine.

It looks you're running this python script under Windows and the path separator for Windows is \ and not /.
You should have instead created the string SUITE_DIR using os.sep

Related

How to manipulate directory paths to work across multiple OS?

I'm writing a python script which has to internally create output path from the input path. However, I am facing issues to create the path which I can use irrespective of OS.
I have tried to use os.path.join and it has its own limitations.
Apart from that, I think simple string concatenation is not the way to go.
Pathlib can be an option but I am not allowed to use it.
import os
inputpath = "C:\projects\django\hereisinput"
lastSlash = left.rfind("\\")
# This won't work as os path join stops at a slash
outputDir = os.path.join(left[:lastSlash], "\internal\morelevel\outputpath")
OR
OutDir = left[:lastSlash] + "\internal\morelevel\outputpath"
Expected output path :
C:\projects\django\internal\morelevel\outputpath
Also, the above code doesn't do it OS Specific where in Linux, the slash will be different.
Is os.sep() some option ?
From the documentation os.path.join can join "one or more path components...". So you could split "\internal\morelevel\outputpath" up into each of its components and pass all of them to your os.path.join function instead. That way you don't need to "hard-code" the separator between the path components. For example:
paths = ("internal", "morelevel", "outputpath")
outputDir = os.path.join(left[:lastSlash], *paths)
Remember that the backslash (\) is a special character in Python so your strings containing singular backslashes wouldn't work as you expect them to! You need to escape them with another \ in front.
This part of your code lastSlash = left.rfind("\\") might also not work on any operating system. You could rather use something like os.path.split to get the last part of the path that you need. For example, _, lastSlash = os.path.split(left).
Assuming your original path is "C:\projects\django\hereisinput", your other part of the path as "internal\morelevel\outputpath" (notice this is a relative path, not absolute), you could always move your primary back one folder (or more) and then append the second part. Do note that your first path needs to contain only folders and can be absolute or relative, while your second path must always be relative for this hack to work
path_1 = r"C:\projects\django\hereisinput"
path_2 = r"internal\morelevel\outputpath"
path_1_one_folder_down = os.path.join(path_1, os.path.pardir)
final_path = os.path.join(path_1_one_folder_down, path_2)
'C:\\projects\\django\\hereisinput\\..\\internal\\morelevel\\outputpath'

Python os.chdir changes "\" to "\\" when passing path/directory path, any ideas to fix?

I am trying to change directory using a variable which contains a path but os.chdir changes the \ in the path to \ which makes it undreadable/undefined for the os module and crashes.
any ideas on how to overcome this please?[shot ][1]
Try to use raw strings when dealing with file paths.
BOOKING_DIR = r"path to your dir"

What is 'subdir' defined var in python 3 for all OS?

I have a script in Python which i get 3 arguments from the user
One of the arguments is a folder path in which there are some files i need to use
Since my program is designed for all OS, i would like to know how to use the path from the argument correctly to get to my required files
I.E.
if i get the following path :
c:\windows
i would like to be able to get 1.exe in this folder,
In windows it will be slash or backslash but in unix systems it will be probably different,
As i understand there is a const or defined var from 'os' module in which i can use as this subdir sign, where can i find it ?
Thank you
Just use os.path.join and Python will take care of the slash for you:
path = os.path.join(sys.argv[1], '1.exe')
The platform-specific path separator is stored as os.sep.
Use os.path.join(). For example:
os.path.join(dirname, "1.exe")

os.path.join(os.path.dirname(__file__)) returns nothing

I have a directory structure like this:
--bin/
--lib/
--data/
So basically, the executable script is in bin and it calls the files in lib.. but lib has to communicate with the text files in data
Usually this use to work:
TO read a file in usually i use to do this
file_path = os.path.join(os.path.dirname(__file__))+ "/../" +"data/"+filename
f = open(file_path,"r")
But, in this instance, if i do:
print os.path.join(os.path.dirname(__file__))
returns nothing?
What am i doing wrong..
Thanks
I guess with nothing you mean an empty string? This could only be the case, if __file__ was an empty string in the first place. Did you accidentally overwrite __file__?
One other comment in addition to the others...the point of os.path.join is to avoid things like
mypath=dir + '/' + subdir + '/'+filename
This is done much more cleanly using
mypath=os.path.join(dir,subdir,filename) # can have as many arguments as you want!
Also, you can avoid explicit '..' and '.' in path names by using os.pardir and os.curdir. (e.g.)
file_path = os.path.join(os.path.dirname(__file__),os.pardir,'data',filename)
This should increase the portability of your code (and is a good habit to get into even if you don't plan on running this script anywhere else).
It depends on how you start your script, for example:
if /bin/script.py contains:
import os
print os.path.dirname(__file__) #no reason to use os.path.join()
then:
$> python /bin/script.py
/bin
$> cd /bin
$> python script.py
#nothing
$>
It'a a better idea to use the following:
file_path = os.path.abspath(__file__)
and then do whatever you want with that.

Python: How to Access Linux Paths

Using Python, how does one parse/access files with Linux-specific features, like "~/.mozilla/firefox/*.default"? I've tried this, but it doesn't work.
Thanks
This
import glob, os
glob.glob(os.path.expanduser('~/.mozilla/firefox/*.default'))
will give you a list of all files ending in ".default" in the current user's ~/.mozilla/firefox directory using os.path.expanduser to expand the ~ in the path and glob.glob to match the *.default file pattern.
~ is expanded by the shell and not a real path. As such you have to navigate there manually.
import os
homeDir = os.environ['HOME']
f = open( homeDir + '/.mozilla/firefox/*.default' )
# ...
It's important to remember:
use of the tilde ~ expands the home directory as per Poke's answer
use of the forward slash / is the separator for linux / *nix directories
by default, *nix systems such as linux for example has a wild card globbing in the shell, for instance echo *.* will return back all files that match the asterisk dot asterisk (as per Will McCutcheon's answer!)
http://docs.python.org/library/os.html
Gives a complete reference if you would like to change directory or give paths.
You can for example give relative paths and access specific files.
If you would like to execute commands then http://docs.python.org/library/commands.html provides nice wrappers for the os.popen() function

Categories

Resources