Python Forum
Visual Studio Code does not print desired output but only prints "..."
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Visual Studio Code does not print desired output but only prints "..."
#1
Hi all,

I'm a Python rookie (doing an online Python for Beginners course via edX) and I'm struggling with the following.
When I want to execute the below-stated lines of code:

fhand = open("mbox-short.txt")
for line in fhand:
    line = line.rstrip()
    if line.startswith("From: "):
        print(line)
, it prints the following:
Vincents-MacBook-Air-4:PYTHON DATA  STRUCTURES vincentolivers$ /usr/bin/python
Python 2.7.10 (default, Aug 17 2018, 19:45:58) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> fhand = open("mbox-short.txt")
>>> 
>>> for line in fhand:
...     line = line.rstrip()
...     if line.startswith("From: "):
...         print(line)
... 
Any ideas on how to print my desired output instead of "...", i.e. all lines that start with "From: " in the handled text file?

Thanks in advance!!
Reply
#2
First - it's recommended to open files with 'with' statement a la with open('mbox-short.txt', 'r') as f:

Second - if you try to enter code in interactive interpreter then you should make sure indentation is correct a la:

>>> for i in range(3):
...     print(i)
...
0
1
2
>>>
Third - it is not recommended to use Python 2 (for example print is quite different between Python 2 and 3)
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
#3
it looks like you perform "Run Selection/Line in Python Terminal" command (from the mouse right-click menu). In this case it execute your script/selection/current line in python interactive mode line by line
You need to use "Run Python File in Terminal" instead
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
@burak thanks for your reply! And I'm sorry for not using that template, I just signed up and quickly wanted to get my problem solved which led to overreading those rules. I will take them into account from now onwards.

Content-wise, I actually do not see any "Run Python File in Terminal" option... Also, I downloaded the latest version of Python multiple times but somehow it keeps on using the 2.7.10 version...
Reply
#5
How do you run your code?
it's in the right-click mouse menu, when click in the code part of the IDE window

as to 2/3 problem - you can select which interpreter to use in VSCode
Ctrl+Shift+P -> find Select Interpreter command
you are on linux paython defaults to python2 version
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I indeed run the code by selecting the lines --> right-click --> "Run Python File in Terminal".
The other option when right-clicking is "Run Current File in Python Interactive Window". However, when I click that option, VSC requires me to install Jyputer. Apparently Jyputer is installed but it can't be run since my Python version is not supported, can that be true?
Reply
#7
   

this is the right-click menu I have. I believe you use the Run selection/line in python terminal
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Mine lacks the 2nd "run" option compared to yours...
Reply
#9
what version of VSCode/Python extension do you use?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Python 2.7.10 64-bit.

Maybe I should consider reinstalling Python since there could be a chance I've unconsciously changed some vital settings or so. Also, how is it possible that even though I downloaded Python 3.7, only the 2.7.10 version pops up as possible interpreter? I'm kinda lost
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 375 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,035 Nov-15-2023, 06:56 PM
Last Post: jst
  How to set PYTHONPATH in Visual Studio Code? aupres 5 4,221 Aug-15-2023, 03:51 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 890 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Visual Studio Code NewPi 3 1,057 May-16-2023, 11:13 PM
Last Post: snippsat
  zfill prints extra et the end of a var tester_V 4 909 Mar-24-2023, 06:59 PM
Last Post: tester_V
  I cannot able to see output of this code ted 1 756 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,095 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Json filter is not capturing desired key/element mrapple2020 1 1,140 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  Visual Studio Code venv ibm_db error mesi1000 7 2,815 Nov-13-2022, 12:36 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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