txt file appears blank with .write() python - python

I am on a windows machine and am trying to write a couple thousand lines to a text file using ipython. To test this I am just trying to get some text to appear in the file.
my code is as follows:
path="\Users\\*****\Desktop"
with open(path+'newheaders.txt','wb') as f:
f.write('new text')
This question (.write not working in Python) is answered and seems like it should have solved my issue but when I open the text file it is still blank.
I tested the file using the code below and the text appears to be there.
with open(path+'newheaders.txt','r') as f:
print f.read()
any ideas?

This 'should' work as written. A few things to try (I would put this in a comment but I lack sufficient reputation):
Delete the file and make sure the program is creating the file
Try writing as 'wt' rather than binary to see if we can narrow down the problem that way.
Remove all the business with the path and just try to write the file in the current directory.
What text editor are you using? Is it possible it's not refreshing the blank file?

Related

How to open a text file which has more than 500k lines, without using any iteration?

I am working with text files and looping over them, python works well with files of 10k to 20k lines, most of them are of that length, few text files are over 100k lines, where the code just stops or just keeps buffering, how can we improve the speed or open the text file directly, even if there has to be any iteration, it should be pretty quick, and I want my text file opened in a string format, so no readlines.
I'm confused as to your "without iteration" parameter. You're already looping over the files and using a simple open or some other method, so what is it that you're wanting to change? As you didn't post your code at all there's nothing to work from to understand what might be happening or to suggest changes to.
Also, it sounds like you're hitting system limits, not a limit of python itself. For a question like this it would be worthwhile to give your system parameters alongside the code so that someone can get a full picture when responding.
Typically I just do something similar to the good ol' standby that won't destroy your memory:
fhand = open('file.extension')
for line in fhand:
# do the thing you need to do with each line
You can see a more detailed explanation here or in Dr Chuck's handy free textbook under the "Files" section.

filenotfounderror: [errno 2] no such file or directory: .txt file

I am currently learning python and my instructor is telling me to open a text file using the open() meathod. I get the following error each time:
FileNotFoundError: [Errno 2] No such file or directory: 'movies.txt'
I have tried using online guides but all I could find was for .csv files, whereas I'm trying to open a text file. My complete code is as follows:
with open('movies.txt') as file_object:
contents = file_object.read()
print(contents.strip())
I've tried writing the file 'movies.txt' in VS code, and in my notepad, and then saving it to the same directory as the code but no use. I have also attempted to use a more exact file source, but still the same error. Sadly, google didn't have too much information for text files so I have to come here. Is there something I have to change in VS settings? I also tried my code in IDLE but not response either.
Thanks,
Stan
First of all you have to make sure that the file you are looking is in the same folder as your script as you are giving just the name and not the path.
Then the code to read a file misses a parameter:
with open('movies.txt', 'r') as file_object:
contents = file_object.read()
print(contents.strip())
r: read
w: write
a: append
The code is ok, the problem is finding the object as the error states. As you write it, it looks like "movies.txt" is in the same directory as the script. Are you sure they are in the same directory? Otherwise you will have to set the whole route
This is not about the directory your code is in. This is about the current working directory. These two directories may or may not be the same in some specific circumstance, but treating them as the same thing will lead to confusion in the future.
I don't know about VS Code (haven't worked with it), but all IDEs I've worked with have an option to set the current working for code you're running from the IDE.
I ended up using the run & debug function in VS, and it works. I dont understand what it debugged. All the information it gave me was my .vscode files? I guess its fine for now? Is it possible that the issue was caused by something else I have downloaded on my laptop?
Thanks,
Stan

Python 3+ How to edit a line in a text file

This has been asked, and it has been answered. Though, the answers do not favor my situation or what I'm trying to achieve.
I have 100's of text files I need to update. I need to update the same one line in each file. I want to open up a text file and only modify a single line. I do not want to re-write the text file using write() or writelines(). I also do not want to use fileinput.input() because that too is re-writing the text file using print statements.
The files contain thousands of lines of critical data and I cannot trust that Python will recreate everything correctly in the files (I understand this will probably never happen). There are several lines that pertain to footer data which is non-critical and that's what I am updating.
How can one update a single line in a text file, without recreating the file. Appending one line in a text must be possible.
Thanks in advance
I don't think this is possible in any programming language at the file level. Files simply don't work that way -- especially text-based files which you are likely replacing with different-length data in the middle. You would need raw disk level access (making this a stupidly difficult problem).
If you really want to pursue it, check the raw disk question here:
Is it possible to get writing access to raw devices using python with windows?
EVEN THEN: I'm pretty sure at some level AT LEAST an entire block of data will be read from the drive and re-written (this is physically how drives work if I recall).

Has .txt file been closed?

I have a slight problem. I have a project that I'm working on and that requires two programs to read/write into some .txt files.
Python writes into one .txt file, C++ reads from it. C++ does what it needs to do and then writes its own information into another .txt file that Python has to read.
What I want to know is how can I check with C++ if Python has closed the .txt file before opening the same file, as Python may still be writing stuff into it and vice versa?
If you need any extra information about this conundrum, feel free to contact me.
whenever, in python, you use:
f.open()
always follow it with
f.close()
then you know its closed
see:
https://docs.python.org/2/tutorial/inputoutput.html
https://www.tutorialspoint.com/python/file_close.htm
re: comment
ETA
How to check if a file has been opened by another application in C++?
Is there a way to check if a file is in use?
C/C++ Standard Function to Check if a file is used by another process?
Way to check in C/C++ if a file is in use?
albeit hacky, I think my favorite after reading through was this one:
if ( 0 != rename("c:/foo.txt", "c:/foo.txt") ) {
printf("already opened\n");
}
https://stackoverflow.com/a/1048721/3680588
You might consider having each "writer" process write its output to a temporary file, close the file, then rename it to the filename that the "reader" process is looking for.
If the file is present, then the respective reader process knows that it can read from it.
Using the with open() method always closes the file after operations are performed. See section 7.2 in the Input and Output documentation.
with open(in_file, 'w') as f:
content = 'Example text'
f.write(content)
Once the above is complete, the file is closed.

Python - how to read .pages document in python?

I am new to python and I am trying to read/edit .pages document in python.
I used this code:
with open(directory+"example.pages") as f:
print(f.read())
However, the output is unreadable. I am trying to guess the root cause of the issue and it seems to be an encoding issue.
['PK\x03\x04\x14\x00\x00\x00\x00\x00}\xa2\x99G^8\xfd\\\x00\x0f\x00\x00\x00\x0f\x00\x00\x12\x00\x00\x00Index/Document.iwa\x00\xfc\x0e\x00\xa75\xf0\xa54\x08\x01\x120\x08\x90N\x12\x03\x01\x00\x05\x18\xb3\x01"']
['\x03']
['\x01', '\x10\x01\x18\x00*\x18\xc2\x13\xa0\x11\x99\x11\x9c\x11\x9f\x11\x9a\x11\xa1\x11\xc3\x13\x9b\x11\x9e\x11\x9d\x11\x98\x11\x12\x03\x08\x9d\x11\x1a\x03\x08\x9f\x11"\x03\x08\xa0\x112\x03\x08\x9c\x11:\x03\x08\xa1\x11z=']
['\x05:\x03\x08\x98\x11\x1a\x02en"\x03\x08\x99\x11*\x03\x08\xc3\x132\x03\x08\x9b\x11:\x03\x08\x9a\x11#\x00J\x15Application/Blank/ISOj\x03\x08\xc2\x13\xa2\x01\x03\x08\x9e\x11\xf5\x01\xec\xd1\x14D\xfd\x01\xf6xRD\x85\x02\x8c\xc5bB\x8d\x05\x06\x00\x95\x05\x06\x00\x9d\x05\x06\xf0J\xa5\x02w\xbb', 'B\xad\x02(\x14*B\xb5\x02\x00\x00\x80?\xb8\x02\x00\xc0\x02\x00\xd0\x02\x00\xda\x02\x07mfp2462\xe2\x02\x06iso-a4\xe8\x02\x00\x0f\x08\xc2\x13\x12']
['\x08\xe0\x04\x12\x03\x01\x05\x00\x18\x00\x19\x08\xa0\x11\x12\x14\x08\xd1\x0f\x12\x05\xf90g*\x08\xd9\x11\xbd\x11\xeb\x11\xec\x11\x08\x00', '\xdf$\x05salut*']
['\x07\x05\x120\xd9\x112\x08']
['\x06\x08\x00\x10\x00\x18\x00:\x11\x15\x10\xbd\x11P\x01b\x11', '\x08\xeb\x11r\x15"\x04\x8a\x01\x11\x16', '\xec\x11\x9a\x01\x05\x17\x10\x12\x02en\xc2', '\x01D\x1c$\x08\xec\x11\x12\x1f\x08\x9b-z\x005)y\xf0L\x1d\x10\x01\x18\x01*\x08\xa5\x13\xa6\x13\xa7\x13\xa4\x13\x88\x01\x01\x90\x01\x00\x98\x01\x00\xa0\x01\x00\.....
I don't know Pages in particular. But a text editor with that much formatting, layout etc is likely to be almost unreadable by humans.
What you are displaying is a lot of characters, most are non-printable, I see a couple commas and spaces but that is it. Have you tried look at the file in a standard text editor (like TextEditor for iOS, Notepad for Windows) to see what it is you are dealing with?

Categories