tags:

views:

181

answers:

3
file_read = open("/var/www/rajaneesh/file/_config.php", "r")
        contents = file_read.read()
        print contents
        file_read.close()

The output is empty, but in that file all contents are there. Please help me how to do read and replace a string in __conifg.php.

+4  A: 

Usually, when there is such kind of issues, it is very useful to start the interactive shell and analyze all commands.

For instance, it could be that the file does not exists (see comment from freiksenet) or you do not have privileges to it, or it is locked by another process.
If you execute the script in some system (like a web server, as the path could suggest), the exception could go to a log - or simply be swallowed by other components in the system.
On the contrary, if you execute it in the interactive shell, you can immediately see what the problem was, and eventually inspect the object (by using help(), dir() or the module inspect). By the way, this is also a good method for developing a script - just by tinkering around with the concept in the shell, then putting altogether.

While we are here, I strongly suggest you usage of IPython. It is an evolution of the standard shell, with powerful aids for introspection (just press tab, or a put a question mark after an object). Unfortunately in the latest weeks the site is not often not available, but there are good chances you already have it installed on your system.

Roberto Liffredo
A: 

Would it be possible that you don't have read access to the file you are trying to open?

Halfcent
Would that not throw an error in itself?
AutomatedTester
had it been the case he would got the `IOError: Permission denied`
SilentGhost
+2  A: 

I copied your code onto my own system, and changed the filename so that it works on my system. Also, I changed the indenting (putting everything at the same level) from what shows in your question. With those changes, the code worked fine.

Thus, I think it's something else specific to your system that we probably cannot solve here (easily).

PTBNL