This is only a question regarding which one would be more "pythonic"  using if:  import os somepath = 'c:\\somedir' filepath = '%s\\thefile.txt' % somepath if not os.path.exists(somepath) and not os.path.isfile(filepath):     os.makedirs(somepath)     open(filepath, 'a').close else:    print "file and dir allready exists"   or using try/Except:  import os somepath = 'c:\\somedir' filepath = '%s\\thefile.txt' % somepath try:     os.makedirs(somepath) except:     print "dir allready exists" try:     with open(filepath):         // do something except:     print "file doens't exist"   As you can see on the examples above, which one would be more correct on python? Also, On which cases should i use try/except instead if/else ? I mean, should i replace all my if/else tests to try/except?  Thanks in advance.
2402
What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:  import os  file_path = "/my/directory/filename.txt" directory = os.path.dirname(file_path)  try:     os.stat(directory) except:     os.mkdir(directory)         f = file(filename)   Somehow, I missed os.path.exists (thanks kanja, Blair, and Douglas). This is what I have now:  def ensure_dir(file_path):     directory = os.path.dirname(file_path)     if not os.path.exists(directory):         os.makedirs(directory)   Is there a flag for "open", that makes this happen automatically?
50133
I would find it very convenient to be able to use cd with a file argument.  cd myDirectory/anyname.anyExtension would be equivalent to cd myDirectory/  What would be the best alias or function to achieve this behavior ?  EDIT: Sorry I didn't mention it in the 1st place: I use zsh
86631
Non existing file  $ ls file_not_exists.txt ls: cannot access file_not_exists.txt: No such file or directory $ echo &lt;&gt; file_not_exists.txt  $ ls file_not_exists.txt  file_not_exists.txt $ cat file_not_exists.txt $   File with content  $ cat temp.txt  asdf $ echo temp.txt  temp.txt $ echo &lt;&gt; temp.txt   $ cat temp.txt  asdf    If the file does not exist, echo &lt;&gt; file_not_exists.txt will create a new file. So I think &gt; works (redirecting the empty output into a newly created file). But if there is something in the file (like temp.txt), why it is not emptied by echo &lt;&gt; temp.txt?
1778
I have a script which sets some environment variable to paths:  export VAR1=/home/xyz/DIR_IN_WHICH_THIS_FILE_EXISTS  export VAR2=$VAR1/abc/qwe  ...   Can I use something to get the DIR_IN_WHICH_THIS_FILE_EXISTS so that if I run this script from anywhere(any path) in the machine, it would set the same values to VAR1 depend on in which directory the script exists?  Please help
52231
If I had a dictionary dict and I wanted to check for dict['key'] I could either do so in a try block (bleh!) or use the get() method, with False as a default value.  I'd like to do the same thing for object.attribute. That is, I already have object to return False if it hasn't been set, but then that gives me errors like     AttributeError: 'bool' object has no attribute 'attribute'
13133
I'm trying to know if a blend file contains images and if it contains to unpack the images out to the directory. So just before I'm running this line: bpy.ops.file.unpack_all(method='USE_ORIGINAL')  to ask if this blend file is with packed images.
115499
I am trying to write any error messages that arise in my script and record it in a text file. When I check my errorLog, the error messages are not recorded. Below is my code: Note the parameter 'filePath' is the file path to the file that produced the error.  except:     addError = arcpy.GetMessages(2)     with open(errorLog, 'a') as errorMsg:         errorMsg.write('%s, %s\n' % (filePath,addError))   Can anyone offer insight?
7809
I would like to test a string containing a path to a file for existence of that file (something like the -e test in Perl or the os.path.exists() in Python) in C#.
23870
I've used the following script to see if a file exists:  #!/bin/bash  FILE=$1      if [ -f $FILE ]; then    echo "File $FILE exists." else    echo "File $FILE does not exist." fi   What's the correct syntax to use if I only want to check if the file does not exist?  #!/bin/bash  FILE=$1      if [ $FILE does not exist ]; then    echo "File $FILE does not exist." fi
60871
I wrote myself a little downloading application so that I could easily grab a set of files from my server and put them all onto a new pc with  a clean install of Windows, without actually going on the net.  Unfortunately I'm having problems creating the folder I want to put them in and am unsure how to go about it.  I want my program to download the apps to program files\any name here\  So basically I need a function that checks if a folder exists, and if it doesn't it creates it.
35461
I'd like to write something like this:  $ ls **.py   in order to get all .py filenames, recursively walking a directory hierarchy.  Even if there are .py files to find, the shell (bash) gives this output:  ls: cannot access **.py: No such file or directory   Any way to do what I want?  EDIT: I'd like to specify that I'm not interested in the specific case of ls, but the question is about the glob syntax.
56356
Pretty simple question: In Linux, why does Python require the line   #!/usr/bin/python   at the start of a python file, since Windows doesn't?  What does it do? 'cause the description "Links to Python" is a bit vague...
13167
In Python, is there a portable and simple way to test if an executable program exists?  By simple I mean something like the which command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with Popen &amp; al and see if it fails (that's what I'm doing now, but imagine it's launchmissiles)
15003
Can you add new statements (like print, raise, with) to Python's syntax?  Say, to allow..  mystatement "Something"   Or,  new_if True:     print "example"   Not so much if you should, but rather if it's possible (short of modifying the python interpreters code)
91508
I am completely new to Linux and Ubuntu.   I have written python code in Windows and want to run it in Ubuntu. It uses a text file for input. The 'path-to-file' in windows looks as follows   c:user\documents\python\file.txt   How is the location written for Ubuntu?
6612
How do I check whether a file exists or not, without using the  statement?
91482
Duplicate:        How do I get the path of a the Python script I am running in? I was doing dirname(sys.argv[0]), however on Mac I only get the filename - not the full path as I do on Windows.  No matter where my application is launched from, I want to open files that are relative to my script file(s).