Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97371

how to read all files in a directory in python with Visual Studio compile

$
0
0

I have the following code that should read all the .txt files in the folder where my project is located (I'm doing it in Visual Studio Code):

for file in glob.glob("*.txt"):
    fp = open(file, "r", -1, 'utf-8')
    text= fp.readlines()
    print(file, "->", text)

I would like to read all the files (without informing the directories) .txt that are in my folder but this function is somehow not working in Visual Studio, but it works perfectly in Jupyter notebook.

Output/Content of the text variable in jupyter notebook

file1.txt -> ["this phrase is in file 1"]
file2.txt -> ["this phrase is in file 2"]
file3.txt -> ["this phrase is in file 3"]

Does anyone know if I'm forgetting a Visual Studio function or configuration or is there another function that does this?


Viewing all articles
Browse latest Browse all 97371

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>