.txt file is acting weird under readlines() - text

The contents of the file look like this:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I run:
the_txt_file = open('/txt_file')
Then I run:
the_txt_file_as_a_list = the_txt_file.readlines()
Then I run:
print the_txt_file_as_a_list
And I get this:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
But I was expecting something like:
['1/15/13,930,1441.5\n','15/13,1000,1442.75\n','15/13,1030,1444\n']
This happens to me pretty frequently, what is going on?

So it seems that the problem had something to do with the way my mac interacted with the .txt file
The problem was fixed by swapping:
the_txt_file = open('/txt_file')
with:
the_txt_file = open('/txt_file', 'rU')
The 'rU' is called 'universal-readline'. Opening a file in 'rU' mode is opening a file in Universal readline mode. Upon running:
the_txt_file_as_a_list = the_txt_file.readlines()
and then:
print the_txt_file_as_a_list
my output went from:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
to:
['1/15/13,930,1441.5\n', '1/15/13,1000,1442.75\n', '1/15/13,1030,1444\n']
Later, I was able to print each item seperatly by:
for item in the_txt_file_as_a_list:
print item
The output looked like:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444

I would assume that you, or the original creator of this data file were on a Mac. Seems you are expecting it to be a simple '\n' line ending, but suffer from the originating editors system default line ending (most likely).
An easy fix, is to call open(...) with the rU option like so:
the_txt_file = open('/txt_file', 'rU')
This ensures that the file is opened read only, and uses Universal newline support when reading the particular file.
Good luck!

Related

system is not writing into original dictionary file

def update(login_info):
stids = 001
file = open('regis.txt', 'r+')
for line in file:
if stids in line:
x = eval(line)
print(x)
c = input('what course you would like to update >> ')
get = x.get(c)
print('This is your current mark for the course', get)
mark = input('What is the new mark? >>')
g = mark.upper()
x.update({c: g})
file.write(str(x))
Before writing into the file
After writing into the file
This is what happens in the idle
As you can see, the system is not writing the data into the original dictionary. How can we improve on that? Pls, explain in detail. Thx all
Python doesn't just make relations like that. In Python's perspective, you are reading a regular text file, executing a command from the line read. That command creates an object which has no relationship to the line it was created from. But writing to the file should still work in my opinion. But you moved a line further (because you read the line where the data was and now you are at the end of it).
When you read a file, the position of where we are on the file changes. Iterating over the file like that (i.e for line in file:) invokes implicitly next() on the file. For efficiency reasons, positioning is disabled (file.tell() will not tell the current position). When you wrote to the file, for some reason you appended the text to the end, and if you test it it will no longer continue the loop even though it is still on the second line.
Reading and writing at the same time looks like an undefined behaviour.
Beginner Python: Reading and writing to the same file

Python - File Handling - open() function

This is my first time here and I hope that it will be a good adventure in here exchanging with you.
It's about file handling. We open a file using the open() function, we read its content with the read() method. Normally, once we use the read() method its displays the full content of the file. Calling two times will be meaningless because reaching the EOF, there is nothing to be displayed. Nevertheless, with the piece of code below I do get two times the same output, whereas I should be having it one time.
# Opening the file and printing the full content of the file
# By default, the open() funnction comes in Read mode
print(open("demofile.txt").read())
# Going back to the begining of the file
#open("demofile.txt").seek(0)
#print("\n")
# Displaying parts of the file
# Displaying the first 5 letters
#print(open("demofile.txt").read(11))
#print(open("demofile.txt").read())
print(open("demofile.txt").read())
# Closing the file
open("demofile.txt").close()
What have I missed here ? Thank you !
This is the right way:
fh = open("demofile.txt")
print(fh.read())
fh.close()
This is the right and pythonic way/method to open and read a file:
with open("demofile.txt") as demoFp:
for line in demoFp:
print(line)
Or another approach by reading the whole lines at once (for small files )
with open("demofile.txt") as demoFp:
lines=demoFp.read()
print(lines)

How to fix Python 3 PyAutoGUI screenshot error? (macOS)

I keep getting an error with any of PyAutoGUI's screenshot taking functions such as:
pyautogui.locateOnScreen('button.png')
pyautogui.pixelMatchesColor(x, y, (r, g, b))
im = pyautogui.screenshot()
The error I get is:
screencapture: cannot write file to intended destination, .screenshot2018-1009_16-43-26-003190.png
Traceback (most recent call last):
File "~/program.py", line 111, in <module>
pyautogui.locateOnScreen('/images/play!.png')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 265, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 331, in _screenshot_osx
im = Image.open(tmpFilename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '.screenshot2018-1009_16-43-26-003190.png'
I don't tell it to or want it to save the new screenshotted image to any directory (and it shouldn't). With the pyautogui.screenshot() function I could manually save it to a real directory in my project, but I don't have an option to do that with the other methods. Any idea on how to fix this?
What I've tried:
I looked at all the documentation I could find online of pyautogui screenshots
Restarting computer
Downgrading versions for Pillow and pyscreeze
EDIT:
I tried it on another mac and got the same error.
Tried it on windows bootcamp (windows on my mac) and it works fine.
possible, very hack-ish fix - I don't actually like this answer but it was a quick and easy fix (done on OSX with Mojave):
PLEASE NOTE: modifying the source code of libraries you don't understand is usually a bad idea, so do so at your own risk! This worked for me, your milage may vary.
Go to your file (your file path may be different, I just copied this from your error):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py
find the line under the function "_screenshot_osx" that looks like
tmpFilename = '.screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
copy it and then comment it out, paste the copied line directly below the commented out original and modify to something like this:
tmpFilename = r'<your preferred screenshot folder here>/screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
save the changes, and see if it works.
Also note: pyautogui.locateOnScreen can be a bit finicky so even if this removes your error you still might not get the coordinates you want (might return none). That might be related to a different issue. To test that part I do this:
import pyautogui
pyautogui.screenshot('testFull.png')
placePos = pyautogui.locateOnScreen('testFull.png')
print(placePos)
even the cursor blinking can mess this up though, and osx has translucent user interfaces so it's kind of annoying to test this perfectly without careful image curation.
I was facing this same issue on MacOS Mojave after changing to Python 3.8.
Here is my solution.
Go the same file mentioned by #Richard W.
There, together with all your 'imports', add the following line so the script can find the tmpFilename folder
dirname = os.path.dirname(__file__)
then, replace the also mentioned line by
tmpFilename = os.path.join(dirname,r'screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f')))

Python 3 combining file open and read commands - a need to close a file and how?

I am working through "Learn Python 3 the Hard Way" and am making code more concise. Lines 11 to 18 of the program below (line 1 starts at # program: p17.py) are relevant to my question. Opening and reading a file are very easy and it is easy to see how you close the file you open when working with the files. The original section is commented out and I include the concise code on line 16. I commented out the line of code that causes an error (on line 20):
$ python3 p17_aside.py p17_text.txt p17_to_file_3.py
Copying from p17_text.txt to p17_to_file_3.py
This is text.
Traceback (most recent call last):
File "p17_aside.py", line 20, in
indata.close()
AttributeError: 'str' object has no attribute 'close'
Code is below:
# program: p17.py
# This program copies one file to another. It uses the argv function as well
# as exists - from sys and os.path modules respectively
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print(f"Copying from {from_file} to {to_file}")
# we could do these two on one line, how?
#in_file = open(from_file)
#indata = in_file.read()
#print(indata)
# THE ANSWER -
indata = open(from_file).read()
# The next line was used for testing
print(indata)
# indata.close()
So my question is should I just avoid the practice of combining commands as done above or is there a way to properly deal with that situation so files are closed when they should be? Is it necessary to deal with the situation of closing a file at all in this situation?
Context manager and with statement is a comfortable way to make sure your file is closed as needed:
with open(from_file) as fobj:
indata = fobj.read()
Nowadays, you can also use Path-like objects and their read_text and read_bytes methods:
# This assumes Path from pathlib has been imported
indata = Path(from_file).read_text()
The error you were seeing... is because you were not trying to close the file, but str into which you've read its content into. You'd need to assign object returned by open a name, and then read from and close that one:
fobj = open(from_file)
indata = fobj.read()
fobj.close() # This is OK
Strictly speaking, you would not need to close that file as dangling file descriptors would be "clobbered" with the process. Esp. in a short example like this, it would be of relatively little concern.
I hope I got the follow up question in comment correctly to extend on this a bit more.
If you wanted a single command, look at the pathtlib.Path example above.
With open as such, you cannot perform read and close in a single operation and without assigning result of open to a variable. As both read and close would have to be performed on the same object returned by open. If you do:
var = fobj.read()
Now, var refers to content read out of the file (so nothing that you could close, would have a close method).
If you did:
open(from_file).close()
After (but also before; at any point), you would simply open that file (again) and close it immediately. BTW. this returns None, just in case you wanted to get the return value. But it would not affect previously open file handles and file-like objects. It would not serve any practical purpose except for perhaps making sure you can open a file.
But again. It's a good practice to perform the housekeeping, but strictly speaking (and esp. in a short code like this). If you did not close the file and relied on the OS to clean-up after your process. It'd work fine.
How about the following:
# to open the file and read it
indata = open(from_file).read()
print(indata)
# this closes the file - just the opposite of opening and reading
open(from_file).close()

file.read() not working as intended in string comparison

stackoverflow.
I've been trying to get the following code to create a .txt file, write some string on it and then print some message if said string was in the file. This is merely a study for a more complex project, but even given it's simplicity, it's still not working.
Code:
import io
file = open("C:\\Users\\...\\txt.txt", "w+") #"..." is the rest of the file destination
file.write('wololo')
if "wololo" in file.read():
print ("ok")
This function always skips the if as if there was no "wololo" inside the file, even though I've checked it all times and it was properly in there.
I'm not exactly sure what could be the problem, and I've spend a great deal of time searching everywhere for a solution, all to no avail. What could be wrong in this simple code?
Oh, and if I was to search for a string in a much bigger .txt file, would it still be wise to use file.read()?
Thanks!
When you write to your file, the cursor is moved to the end of your file. If you want to read the data aferwards, you'll have to move the cursor to the beginning of the file, such as:
file = open("txt.txt", "w+")
file.write('wololo')
file.seek(0)
if "wololo" in file.read():
print ("ok")
file.close() # Remember to close the file
If the file is big, you should consider to iterate over the file line by line instead. This would avoid that the entire file is stored in memory. Also consider using a context manager (the with keyword), so that you don't have to explicitly close the file yourself.
with open('bigdata.txt', 'rb') as ifile: # Use rb mode in Windows for reading
for line in ifile:
if 'wololo' in line:
print('OK')
else:
print('String not in file')

Resources