Vsc is giving an error but the python idle doesent?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?How do I search for files in Visual Studio Code?

Why do Russians call their women expensive ("дорогая")?

What is game ban VS VAC ban in steam?

How can I offer a test ride while selling a bike?

Rotated Position of Integers

Can a rogue effectively triple their speed by combining Dash and Ready?

Is the world in Game of Thrones spherical or flat?

Draw a checker pattern with a black X in the center

The deliberate use of misleading terminology

Socratic Paradox

Why does the UK have more political parties than the US?

Different PCB color ( is it different material? )

Why to use water tanks from Space shuttle in museum?

Points within polygons in different projections

Is there an explanation for Austria's Freedom Party virtually retaining its vote share despite recent scandal?

Adding strings in lists together

What is the 中 in ダウンロード中?

How was Apollo supposed to rendezvous in the case of a lunar abort?

Mother abusing my finances

When a current flow in an inductor is interrupted, what limits the voltage rise?

What are the benefits of cryosleep?

Do creatures all have the same statistics upon being reanimated via the Animate Dead spell?

The qvolume of an integer

Thousands and thousands of words

Could IPv6 make NAT / port numbers redundant?



Vsc is giving an error but the python idle doesent?


Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?How do I search for files in Visual Studio Code?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I wrote a program, and when I run it in Visual Studio Code it gives an error but not when I run it in the Python IDLE. I have set up the environment variable but it still doesn't work. So can u please tell me how to fix this



This also has happened when I import a file and all sorts of places where I want to use a different file



This is my compiler.py file



fileName = "file.txt"

file = open("file.txt", "r+")

def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1

for loop in range(file_len(fileName) + 1) :
print(loop)


and this is my file.txt



hallo


When I run this in Visual Studio Code it gives this error



PS C:UsersHarry KrugerDocumentscode> & "C:/Program Files (x86)/Python37-32/python.exe" "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py"
hallo
Traceback (most recent call last):
File "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py", line 4, in <module>
file = open("file.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'


And when i run ir in python IDLE it works and the output is this



0
1









share|improve this question






















  • Please if you don't understand something about my program just ask me I'm not the best programmer.

    – Harry Kruger
    Mar 24 at 10:11






  • 2





    Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

    – Shiva
    Mar 24 at 10:18











  • @Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

    – Harry Kruger
    Mar 24 at 10:24


















1















I wrote a program, and when I run it in Visual Studio Code it gives an error but not when I run it in the Python IDLE. I have set up the environment variable but it still doesn't work. So can u please tell me how to fix this



This also has happened when I import a file and all sorts of places where I want to use a different file



This is my compiler.py file



fileName = "file.txt"

file = open("file.txt", "r+")

def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1

for loop in range(file_len(fileName) + 1) :
print(loop)


and this is my file.txt



hallo


When I run this in Visual Studio Code it gives this error



PS C:UsersHarry KrugerDocumentscode> & "C:/Program Files (x86)/Python37-32/python.exe" "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py"
hallo
Traceback (most recent call last):
File "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py", line 4, in <module>
file = open("file.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'


And when i run ir in python IDLE it works and the output is this



0
1









share|improve this question






















  • Please if you don't understand something about my program just ask me I'm not the best programmer.

    – Harry Kruger
    Mar 24 at 10:11






  • 2





    Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

    – Shiva
    Mar 24 at 10:18











  • @Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

    – Harry Kruger
    Mar 24 at 10:24














1












1








1








I wrote a program, and when I run it in Visual Studio Code it gives an error but not when I run it in the Python IDLE. I have set up the environment variable but it still doesn't work. So can u please tell me how to fix this



This also has happened when I import a file and all sorts of places where I want to use a different file



This is my compiler.py file



fileName = "file.txt"

file = open("file.txt", "r+")

def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1

for loop in range(file_len(fileName) + 1) :
print(loop)


and this is my file.txt



hallo


When I run this in Visual Studio Code it gives this error



PS C:UsersHarry KrugerDocumentscode> & "C:/Program Files (x86)/Python37-32/python.exe" "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py"
hallo
Traceback (most recent call last):
File "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py", line 4, in <module>
file = open("file.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'


And when i run ir in python IDLE it works and the output is this



0
1









share|improve this question














I wrote a program, and when I run it in Visual Studio Code it gives an error but not when I run it in the Python IDLE. I have set up the environment variable but it still doesn't work. So can u please tell me how to fix this



This also has happened when I import a file and all sorts of places where I want to use a different file



This is my compiler.py file



fileName = "file.txt"

file = open("file.txt", "r+")

def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1

for loop in range(file_len(fileName) + 1) :
print(loop)


and this is my file.txt



hallo


When I run this in Visual Studio Code it gives this error



PS C:UsersHarry KrugerDocumentscode> & "C:/Program Files (x86)/Python37-32/python.exe" "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py"
hallo
Traceback (most recent call last):
File "c:/Users/Harry Kruger/Documents/code/quicks/compiler.py", line 4, in <module>
file = open("file.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'


And when i run ir in python IDLE it works and the output is this



0
1






python visual-studio-code






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 9:58









Harry KrugerHarry Kruger

133




133












  • Please if you don't understand something about my program just ask me I'm not the best programmer.

    – Harry Kruger
    Mar 24 at 10:11






  • 2





    Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

    – Shiva
    Mar 24 at 10:18











  • @Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

    – Harry Kruger
    Mar 24 at 10:24


















  • Please if you don't understand something about my program just ask me I'm not the best programmer.

    – Harry Kruger
    Mar 24 at 10:11






  • 2





    Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

    – Shiva
    Mar 24 at 10:18











  • @Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

    – Harry Kruger
    Mar 24 at 10:24

















Please if you don't understand something about my program just ask me I'm not the best programmer.

– Harry Kruger
Mar 24 at 10:11





Please if you don't understand something about my program just ask me I'm not the best programmer.

– Harry Kruger
Mar 24 at 10:11




2




2





Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

– Shiva
Mar 24 at 10:18





Where is the file.txt located? Have you checked the cwd(current working directory) when running the code under VSCode? Based on the pathname in your code, file.txt should be located in the same directory as compiler.py.

– Shiva
Mar 24 at 10:18













@Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

– Harry Kruger
Mar 24 at 10:24






@Shiva hmm im not sure what u mean but it is in the same folder as compiler.py

– Harry Kruger
Mar 24 at 10:24













1 Answer
1






active

oldest

votes


















1














I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.

However in VS Code you seem to run python in the directory C:UsersHarry KrugerDocumentscode where the file.txt is not present and therefore your code fails.

To fix this and run your code in VS Code you have two options:



  1. In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.

  2. You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:

    from os import path
    with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:


    this snipet will look for the absoulte path of the file and open it.





share|improve this answer























  • your option 1 is easy but option 2 is greek so I'll just do 1

    – Harry Kruger
    Mar 24 at 10:35











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322581%2fvsc-is-giving-an-error-but-the-python-idle-doesent%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.

However in VS Code you seem to run python in the directory C:UsersHarry KrugerDocumentscode where the file.txt is not present and therefore your code fails.

To fix this and run your code in VS Code you have two options:



  1. In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.

  2. You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:

    from os import path
    with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:


    this snipet will look for the absoulte path of the file and open it.





share|improve this answer























  • your option 1 is easy but option 2 is greek so I'll just do 1

    – Harry Kruger
    Mar 24 at 10:35















1














I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.

However in VS Code you seem to run python in the directory C:UsersHarry KrugerDocumentscode where the file.txt is not present and therefore your code fails.

To fix this and run your code in VS Code you have two options:



  1. In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.

  2. You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:

    from os import path
    with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:


    this snipet will look for the absoulte path of the file and open it.





share|improve this answer























  • your option 1 is easy but option 2 is greek so I'll just do 1

    – Harry Kruger
    Mar 24 at 10:35













1












1








1







I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.

However in VS Code you seem to run python in the directory C:UsersHarry KrugerDocumentscode where the file.txt is not present and therefore your code fails.

To fix this and run your code in VS Code you have two options:



  1. In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.

  2. You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:

    from os import path
    with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:


    this snipet will look for the absoulte path of the file and open it.





share|improve this answer













I guess that you run python IDLE in the directory "c:/Users/Harry Kruger/Documents/code/quicks. Therefore your code will pass because in this directory is (probably?) also the file.txt.

However in VS Code you seem to run python in the directory C:UsersHarry KrugerDocumentscode where the file.txt is not present and therefore your code fails.

To fix this and run your code in VS Code you have two options:



  1. In VS Code powershell navigate to the directory that contains the file.txt. In your case you should be able to do it by entering cd "c:/Users/Harry Kruger/Documents/code/quicks" and then call your code.

  2. You can modify your code to use the absolute path of the file. Then you can invoke it from any directory. To do so you have to modify the with open() statement. Replace it with:

    from os import path
    with open(path.join(path.abspath(path.dirname(__file__)), 'file.txt'), 'r+') as f:


    this snipet will look for the absoulte path of the file and open it.






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 10:23









HaaLeoHaaLeo

2,2402825




2,2402825












  • your option 1 is easy but option 2 is greek so I'll just do 1

    – Harry Kruger
    Mar 24 at 10:35

















  • your option 1 is easy but option 2 is greek so I'll just do 1

    – Harry Kruger
    Mar 24 at 10:35
















your option 1 is easy but option 2 is greek so I'll just do 1

– Harry Kruger
Mar 24 at 10:35





your option 1 is easy but option 2 is greek so I'll just do 1

– Harry Kruger
Mar 24 at 10:35



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322581%2fvsc-is-giving-an-error-but-the-python-idle-doesent%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript