I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use the code in Windows and Linux.
In python I am using this code:
pathfile=os.path.dirname(templateFile)
rootTree.write(""+pathfile+"/output/log.txt")
When I will use my code in suppose windows machine my code will not work.
How do I use "/" (directory separator) in both Linux and Windows?
How to use "/" (directory separator) in both Linux and Windows in Python? dirname: Questions
os.path.dirname(__file__) returns empty
2 answers
I want to get the path of the current directory under which a .py file is executed.
For example a simple file D: est.py
with code:
import os
print os.getcwd()
print os.path.basename(__file__)
print os.path.abspath(__file__)
print os.path.dirname(__file__)
It is weird that the output is:
D:
test.py
D: est.py
EMPTY
I am expecting the same results from the getcwd()
and path.dirname()
.
Given os.path.abspath = os.path.dirname + os.path.basename
, why
os.path.dirname(__file__)
returns empty?
Answer #1
Because os.path.abspath = os.path.dirname + os.path.basename
does not hold. we rather have
os.path.dirname(filename) + os.path.basename(filename) == filename
Both dirname()
and basename()
only split the passed filename into components without taking into account the current directory. If you want to also consider the current directory, you have to do so explicitly.
To get the dirname of the absolute path, use
os.path.dirname(os.path.abspath(__file__))
What is the difference between os.path.basename() and os.path.dirname()?
2 answers
What is the difference between os.path.basename()
and os.path.dirname()
?
I already searched for answers and read some links, but didn"t understand. Can anyone give a simple explanation?
Answer #1
Both functions use the os.path.split(path)
function to split the pathname path
into a pair; (head, tail)
.
The os.path.dirname(path)
function returns the head of the path.
E.g.: The dirname of "/foo/bar/item"
is "/foo/bar"
.
The os.path.basename(path)
function returns the tail of the path.
E.g.: The basename of "/foo/bar/item"
returns "item"
From: http://docs.python.org/3/library/os.path.html#os.path.basename
How to use "/" (directory separator) in both Linux and Windows in Python? log: Questions
Python"s equivalent of && (logical-and) in an if-statement
5 answers
Here"s my code:
def front_back(a, b):
# +++your code here+++
if len(a) % 2 == 0 && len(b) % 2 == 0:
return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):]
else:
#todo! Not yet done. :P
return
I"m getting an error in the IF conditional.
What am I doing wrong?
Answer #1
You would want and
instead of &&
.
Answer #2
Python uses and
and or
conditionals.
i.e.
if foo == "abc" and bar == "bac" or zoo == "123":
# do something
How do you get the logical xor of two variables in Python?
5 answers
How do you get the logical xor of two variables in Python?
For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string):
str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
print "ok"
else:
print "bad"
The ^
operator seems to be bitwise, and not defined on all objects:
>>> 1 ^ 1
0
>>> 2 ^ 1
3
>>> "abc" ^ ""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: "str" and "str"
Answer #1
If you"re already normalizing the inputs to booleans, then != is xor.
bool(a) != bool(b)