Re: A silly question on file opening
On Feb 11, 1:57 am, Anthony Tolle wrote: > On Feb 10, 3:42 pm,joy99 wrote: > > > Dear Group, > > [snip] > > I tried to change the location to D:\file and as I saw in Python Docs > > the file reading option is now "r+" so I changed the statement to > > file_open=open("D:\file","r+") > > but it is still giving error. > > Only use "r+" if you need to also write to the file. "r" is still > good for opening for reading. > > Without seeing a traceback, I can only assume the error is caused by > using a backslash in the path without escaping it. Try either the > following: > > file_open=open("D:\\file","r+") > > file_open=open(r"D:\file","r+") > > For an explanation, see the Python documentation: > > http://docs.python.org/reference/lexical_analysis.html#string-literals Dear Group, I am sorry I could not report. I was trying your solutions, I could try only one or two, they are working. My problem got solved. Thank you for your suggestions. But soon I find time, I would try all of them, so that I can learn more and what I try to do in this room to get better angles over any problem from experts like you. Thank you for giving me your valuable time despite your busy schedule. Wishing you happy day ahead, Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: A silly question on file opening
On Wed, 10 Feb 2010 21:23:08 +, Steven D'Aprano wrote: > The solution to this is to remember that Windows accepts forward slashes > as well as backslashes, and always use the forward slash. So try: > > open("D:/file") > > and see if that works. The solution is not to hard-code pathnames in your source file. Read the base directory from an environment variable, registry key, or configuration file, and use os.path.join() to construct paths relative to that directory. On Unix, there are situations which require hard-coding pathnames, e.g. the path to the package's system-wide configuration file. But on Windows, there isn't a single directory whose name is fixed. "Windows" isn't guaranteed to be on "C:", and the other standard directories typically have names which vary by language. Hard-coding "Program Files" is a quick way to guarantee that your software won't work on systems using a language other than English. -- http://mail.python.org/mailman/listinfo/python-list
Re: A silly question on file opening
On Wed, 10 Feb 2010 12:42:17 -0800, joy99 wrote: > I tried to change the location to D:\file and as I saw in Python Docs > the file reading option is now "r+" so I changed the statement to >file_open=open("D:\file","r+") > but it is still giving error. You should copy and paste (do not re-type!) the *exact* line that leads to an error, and the full traceback that Python prints. But in this case I can guess what the problem is. Like most C-inspired languages, Python uses backslashes to put in special characters: e.g. \n for newline, \t for tab, and \f for formfeed. So when you try to open "D:\file" you are actually looking for a file on D drive called (chr(12) + "ile"), not "file". The solution to this is to remember that Windows accepts forward slashes as well as backslashes, and always use the forward slash. So try: open("D:/file") and see if that works. >>> print "D:\file" D: ile >>> print "D:/file" D:/file -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: A silly question on file opening
On Feb 10, 3:42 pm, joy99 wrote: > Dear Group, > [snip] > I tried to change the location to D:\file and as I saw in Python Docs > the file reading option is now "r+" so I changed the statement to > file_open=open("D:\file","r+") > but it is still giving error. Only use "r+" if you need to also write to the file. "r" is still good for opening for reading. Without seeing a traceback, I can only assume the error is caused by using a backslash in the path without escaping it. Try either the following: file_open=open("D:\\file","r+") file_open=open(r"D:\file","r+") For an explanation, see the Python documentation: http://docs.python.org/reference/lexical_analysis.html#string-literals -- http://mail.python.org/mailman/listinfo/python-list
A silly question on file opening
Dear Group, I was using Python with IDLE as GUI for quite some time. My Operating System was Windows XP with Service Pack2. Recently I changed the Operating System to Windows XP with Service Pack3. I had to reinstall Python for which I downloaded "python-2.6.4.msi"and loaded it in my D drive. Here, as I am trying to open the file if I am giving a statement like, file_open=open("python26/file","r") It is showing an error, On checking how the files are stored it is showing that it is storing in D drive directly but not in Python folder in D drive there is no Python folder at all. I tried to change the location to D:\file and as I saw in Python Docs the file reading option is now "r+" so I changed the statement to file_open=open("D:\file","r+") but it is still giving error. Did I install it wrongly? If you can please help it out. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list