Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
invalid syantx
#1
I am new in programing , I use python 3.7.2- when I write any code in python file and make RUN I have invalid syntax message
forexample:
I have file with name new file.py I write this :
f=open('bbb.txt', 'w')
file.Write('hello')
file.close()
make RUN then I receive invalid syantax

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

>>> f=open('new.txt','w')
>>> f.write(h'hello')
>>> f.close()

>>> import os
>>> if os.path.exists('newfolder'):
print('yes')
else:
print('no')

>>> if not os.path.exists('folderst'):
os.mkdir('folderst')
Reply
#2
There is an h in front of the string literal 'hello' where you write to the file.
Output:
>>> h"This string literal is syntactically invalid" SyntaxError: invalid syntax >>> r"This is a syntactically valid raw string." 'This is a syntactically valid raw string.' >>> b"This is a syntactically valid bytes literal." b'This is a syntactically valid bytes literal.' >>> "This is a syntactically valid string literal." 'This is a syntactically valid string literal.'
Reply
#3
I also observe that in file you have file.Write('hello'). Shouldn't it be .write?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
I noticed that too, perfringo. I assumed that in his actual code he typed it correctly because it is a lowercase w in his copy paste of the python console. Also, he isn't talking about getting a NameError, he's getting a syntax error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,857 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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