0

I have the following method in Python.

def get_rum_data(file_path, query):
    if file_path is not None and query is not None:
        command = FETCH_RUM_COMMAND_DATA % (constants.RUM_JAR_PATH, 
                                            constants.RUM_SERVER, file_path, 
                                            query)
        print command
        execute_command(command).communicate()

Now inside get_rum_data I need to create the file if it does not exists, if it exists I need to append the data. How to do that in python.

I tried, open(file_path, 'w') , which gave me an exception.

Traceback (most recent call last):
  File "utils.py", line 180, in <module>
    get_rum_data('/User/rokumar/Desktop/sample.csv', '\'show tables\'')
  File "utils.py", line 173, in get_rum_data
    open(file_path, 'w')
IOError: [Errno 2] No such file or directory: '/User/rokumar/Desktop/sample.csv'

I though open would create file in write mode.

4
  • 1
    You'll often see this error if the parent directory (/User/rokumar/Desktop) does not exist. My guess is that /User should be /Users... Commented Jan 6, 2015 at 11:09
  • Create an empty project that has this single line open('/path/to/file', 'w'). Does it fail? Commented Jan 6, 2015 at 11:09
  • You are right . It should be Users Commented Jan 6, 2015 at 11:28
  • Another useful feature for resolving the current user's base directory (works for unix, linux and windows) is using expanduser from the os.path module. Commented Jan 6, 2015 at 11:59

3 Answers 3

2

You can check if all directories in file_path exist prior to trying to write a file.

import os

file_path = '/Users/Foo/Desktop/bar.txt' 
print os.path.dirname(file_path)  
# /Users/Foo/Desktop

if not os.path.exists(os.path.dirname(file_path)):
    os.mkdirs(os.path.dirname(file_path))  
    # recursively create directories if necessary

with open(file_path, "a") as my_file:
    # mode a will either create the file if it does not exist
    # or append the content to its end if it exists.
    my_file.write(your_text_to_append)

-- Edit: Small and probably unnecessary extension --

expanduser:
In your case, as it turned out that the initial problem was a missing s in the path to the user directory, there is a useful feature for resolving the current users base directory (works for unix, linux and windows): see expanduser from the os.path module. With that you could write your path as path = '~/Desktop/bar.txt' and the tilde (~) will be expanded just like on your shell. (with the additional benefit that if you started your script from another user it would then expand to her home directory.

App config directory:
Since in most cases it is not desirable to write a file to the desktop (*nix systems for instance might not have a desktop installed), there is a great utility function in the click package. If you look at the get_app_dir() function on Github, you can see how they provide expanding to an appropriate app dir and supporting multiple operating systems (the function has no dependencies besides the WIN variable that is defined in the _compat.py module as WIN = sys.platform.startswith('win') and the _posixify() function defined on line 17. Often that's a good starting point for defining an app directory to store certain data in.

Sign up to request clarification or add additional context in comments.

Comments

0

It shall be as simple as:

fname = "/User/rokumar/Desktop/sample.csv"
with open(fname, "a") as f:
    # do here what you want
# it will get closed at this point by context manager

But I would suspect, that you are trying to use not existing directory. Usually, "a" mode creates the file if it can be created.

Make sure, the directory exists.

3 Comments

I think the write mode needs to be a if the OP wants to append new data rather than to truncate everything in the existing file?
OP said it needs to treat the exception if the directory doesn't exist
@IuriGuilherme - No, OP did not ask in his question to create the directory. It only asked for creating the file.
0
import os

def get_rum_data(file_path, query):
    if file_path is not None and query is not None:

        if not os.path.exists(os.path.dirname(file_path)) and os.path.dirname(file_path) not in [None, '', ' ']:
            os.makedirs(os.path.dirname(file_path))
        if not os.path.isfile(file_path):
            with open(file_path, 'x') as fp:
                pass

        command = FETCH_RUM_COMMAND_DATA % (constants.RUM_JAR_PATH, 
                                            constants.RUM_SERVER, file_path, 
                                            query)
        print command
        execute_command(command).communicate()

Bibliography:


Just creating an empty file if it doesn't exist:

if not os.path.isfile(file_path):
    with open(file_path, 'x') as fp:
        pass

Creating the file's full absolute path recursively if any directory or subdirectory doesn't exist:

if not os.path.exists(os.path.dirname(file_path)) and os.path.dirname(file_path) not in [None, '', ' ']:
    os.makedirs(os.path.dirname(file_path))

If you don't include the test for empty dirnames (which is the case when file_path is 'example.txt' instead of '/path/to/example.txt', it will raise an exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.