Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write method not working
#1
Hello,

I am trying to create a text file and write to the text file, like a data log, from my python program. However, when I try to do so it seems the "write" method is not working. The text file gets created the way it should but there is no writing inside the log file. Please help.

Here is a simple example of this which I have tried:
f = open(“testfile.txt”,”w”) 
f.write(“Hello World”) 
f.close()
Reply
#2
Using Python 3.6

I changed the double quotes to single quotes and it works fine.

Try that

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#3
You are using some sort of weird 'double quotes'. What program are you using to write your scripts?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Those are called quotations. It was my understanding that in Python 2.6 it doesn't make a difference whether you use double or single. Besides, I have tried both double and single quotes.
Reply
#5
This kind of thing is much more likely to happen if (1) you're copy-pasting or (2) you're using something weird to edit your code. If you use an editor meant for coding, and you're typing things in, you probably won't have this issue.
Reply
#6
(Jul-24-2017, 01:14 PM)Raudert Wrote: Those are called quotations. It was my understanding that in Python 2.6 it doesn't make a difference whether you use double or single. Besides, I have tried both double and single quotes.


 Those are not double quotes. It might be the unicode character for double quotations, though. There is a difference. One of them is on your keyboard, the other isn't.

http://shapecatcher.com/unicode/info/8220
tl;dr: “ is not the same as "

A bad text editor (such as Microsoft Word) will auto-convert double quotes into this misshapen abomination.
Reply
#7
That's probably from whoever edited my post bud

Hold shift and hit that quotation button

I'll clear it up for you bud, and I said this earlier. It doesn't make a difference if I use single our double quotations. Upon BRIEF investigation one might figure this slip of code was wrote and edited by two different people. That would be where your "quotation" struggle lies. LOL
Reply
#8
Quote:That's probably from whoever edited my post bud
I looked at the original text (prior to any edits), the wierd quotes are there
In fairness, There was a new editor introduced, which may have been the reason
Reply
#9
(Jul-27-2017, 05:01 PM)Raudert Wrote: That's probably from whoever edited my post bud

Hold shift and hit that quotation button

I'll clear it up for you bud, and I said this earlier. It doesn't make a difference if I use single our double quotations. Upon BRIEF investigation one might figure this slip of code was wrote and edited by two different people. That would be where your "quotation" struggle lies. LOL  

As the one who edited your original post, I can say no one altered your code. The first edit was to add the code tags and the second was to add the warning about using code tags.
Lars, this was posted before they began working on the new editor.

Python does not care if you use single or double quotes, provided they are those defined by ASCII decimal code 34  (u\0022) for a double quote and ASCII decimal code 39 (\u0027) for a single quote. Meaning both the double and single quotes are 'vertical' Using those definitions, your code runs fine.

However, in your original post, you are using unicode characters \u201C and \u201D for opening and closing quotes.  The respective single version would be \u2018 and \u2019, none of which falls in the ASCII character set.

As was pointed out, this is typically caused by using a word processor such as MS Word rather than a plain text editor such as MS Notepad. Either that or you are working on an extremely old, old computer.

Hope this clears things up for you.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Custom method to handle exceptions not working as expected gradlon93 3 1,019 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  PyAutogui write Dollar Sign Dutch keyboard not working alato 0 797 Nov-22-2022, 11:25 PM
Last Post: alato
  pyspark parallel write operation not working aliyesami 1 1,684 Oct-16-2021, 05:18 PM
Last Post: aliyesami
  remove elements method not working spalisetty06 4 2,465 Aug-13-2020, 01:17 PM
Last Post: deanhystad
  python os.popen is not working for wait method elenaflorence87 0 1,998 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  file.write not working properly mnh001 11 4,453 Nov-09-2019, 10:20 PM
Last Post: mnh001
  Polymorphism not working with a call to a abstract method colt 3 2,319 Nov-04-2019, 11:04 PM
Last Post: colt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020