0

My teacher gave me two text files (tweets.txt and keywords.txt) that I need to do an analysis on. I have downloaded these text files on my computer. How do I import these text files onto my pycharm project so that I can reference them in an input("enter the name of the file:") ? If it matters, I am working off of a macbook with the latest version of python. Thanks!

Ayaet Rakem
  • 9
  • 1
  • 3

3 Answers3

1

Sometimes you won't need to ask in Stack Overflow, just google it, anyways

dir = input("Directory ending with file")
f = open(dir+"txt", "r")

f is stored as a file So I suppose you need a String then do

plain_text_file = f.read()

For good practises close the file later f.close() Check this for more

mTvare
  • 327
  • 1
  • 2
  • 14
0

Try placing the text files in the same directory your python script is in. Then you can access the file by using the open function. For example:

f = open('tweets.txt','r')
contents = f.read()
f.close()

or:

with open('tweets.txt','r') as f:
    contents = f.read()
Epic Gamer
  • 101
  • 1
  • 10
0

You can us numpy as follows

import numpy as np
txt_file = input("File directory/file.txt")
np.loadtxt(txt_file)

Look here for more details on this approach and a number of variations for using this function.

Supremum
  • 1
  • 4