linkyndy
linkyndy

Reputation: 17928

File does not exist error with 'w' mode

I am encountering an odd behaviour from the file() builtin. I am using the unittest-xml-reporting Python package to generate results for my unit tests. Here are the lines that open a file for writing, a file which (obviously does not exist):

report_file = file('%s%sTEST-%s.xml' % \
                (test_runner.output, os.sep, suite), 'w')

(code is taken from the package's Github page)

However, I am given the following error:

...
File "/home/[...]/django-cms/.tox/pytest/local/lib/python2.7/site-packages/xmlrunner/__init__.py", line 240, in generate_reports
(test_runner.output, os.sep, suite), 'w')
IOError: [Errno 2] No such file or directory: './TEST-cms.tests.page.NoAdminPageTests.xml'

I found this weird because, as the Python docs state, if the w mode is used, the file should be created if it doesn't exist. Why is this happening and how can I fix this?

Upvotes: 0

Views: 952

Answers (3)

linkyndy
linkyndy

Reputation: 17928

It seems like the file which needed to be created was attempted to be created in a directory that has already been deleted (since the path was given as . and most probably the test directory has been deleted by that point).

I managed to fix this by supplying an absolute path to test_runner.output and the result files are successfully created now.

Upvotes: 0

Davidmh
Davidmh

Reputation: 3865

file will create a file, but not a directory. You have to create it first, as seen here

Upvotes: 2

Dima Tisnek
Dima Tisnek

Reputation: 11779

from man 2 read

   ENOENT O_CREAT  is  not  set  and the named file does not exist.  Or, a
          directory component in pathname does not exist or is a  dangling
          symbolic link.

take your pick :)

in human terms:

  • your current working directory, ./ is removed by the time this command is ran,
  • ./TEST-cms.tests.page.NoAdminPageTests.xml exists but is a symlink pointing to nowhere
  • "w" in your open/file call is somehow messed up, e.g. if you redefined file builtin

Upvotes: 2

Related Questions