Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong in this code
#1
import sys
print("open_file.py")
content=dir(sys)
print(content)

rep = 'y'
while (rep != 'n'):
    name = input('Enter the name of the file:')
    print('You entered: ', name)
    name = '\Documents\Py2\' + name
    print('name is: ', name)
    try:
        fh = open(name, "r+")
    except IOError:
        print("Cannot find file ")
    else:
        str = fh.read(10);
        print("Read String is : ", str)

        # Check current position
        position = fh.tell();
        print("Current file position : ", position)

        # Reposition pointer at the beginning once again
        position = fh.seek(0, 0);
        str = fh.read(140);
        print("Again read String is : ", str)
        fh.close()
        rep = input('Try for another file ?  y/n ? ')
        if rep == 'n':
            print("Finished with this file")
        else:
            rep = 'y'

print('READING THE WHOLE FILE')
name=input('Enter the name of the file: ')
name='/home/sylvain/'+name
f=open(name)
text=f.read()
print(text)
f.close()
Error:
E:\Documents\Py2>python open_file.py File "open_file.py", line 11 name = '\Documents\Py2\' + name ^ SyntaxError: EOL while scanning string literal
[/python]
Reply
#2
I guess you try to run it with python2 (based on folder name Py2
actually it is python3 code.

Apart from that it need great deal of improvment, correction. e.g. you use builtin function str as variable name, thus overshadowing it (i.e. you will not be able to use it if needed)
Reply
#3
Glad to meet you Buran. It is strange enough. This code worked well with Pycharm. Now with Atom and python 3.6, it doesn't. I am sure I use 3.6:
E:\Documents\Py2>python -V
Python 3.6.4
Reply
#4
Sorry, it was before my morning coffee
the problem is the backslash at the end of the path, because it escape the closing quote

you can change line 10 to use forward slash
    name = '/Documents/Py2/' + name
or escape the backslash

    name = '\\Documents\\Py2\\' + name
or use raw string

    name = r'\Documents\Py2\' + name
or use os.path.join() to construct the path
Reply
#5
name = '\\Documents\\Py2\\' + name
This works well. I am amazed because I never met that syntax, in Lutz's book. Before we leave, please answer this question: does your Pycharm accept tkinter or pygame imports ?? My Pycharm unfortunately does not accept them.
Reply
#6
(Mar-02-2018, 08:19 AM)sylas Wrote: Before we leave, please answer this question: does your Pycharm accept tkinter or pygame imports ?? My Pycharm unfortunately does not accept them.
You have started multiple separate threads and these question was explained to you multiple times. You need to put in some effort and learn PyCharm if you continue to insist using it. We can help, but we cannot funnel it in your head!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 515 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 893 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,626 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,306 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,878 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,646 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,558 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,649 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,543 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,131 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

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