On Monday, November 11, 2013 4:26:46 PM UTC-6, Matt wrote: > So I want to take the file, "desktop/test.txt" and write > it to "desktop/newfolder/test.txt". I tried the below > script, and it gave me: "IOError: [Errno 2] No such file > or directory: 'desktop/%s.txt'". Any suggestions would be > great. > > def firstdev(file): > in_file = open("desktop/%s.txt") % file > indata = in_file.read() > out_file = open("desktop/newfolder/%s.txt", 'w') % file > out_file.write(indata) > out_file.close() > in_file.close() > firstdev("test")
1. i believe win32 file paths require a qualifying volume letter. 2. Never, ever, *EVER* write data to disc before confirming the paths your passing are pointing to the location you intended to write the data. Use os.path.exists(path) to test your paths BEFORE trying to write data. 3. Be sure your variables names are both "self documenting" and "non clobbering". psst: "file" is a builtin! Using "filename" would be a far wiser choice for a variable containing a filename. When i see "file", i think of a "file object" 4. When dealing with files you must be sure that exceptions are handled cleanly. You don't want open file objects floating aimlessly around in memory because your naive code blew chunks. 5. Remember, you cannot write a file into a directory that does not exist. 6 For OS compatibility always use os.path.join() to join path parts into a whole. This method will insert the proper separator for you depending on the OS. -- https://mail.python.org/mailman/listinfo/python-list