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!
            Asked
            
        
        
            Active
            
        
            Viewed 5,177 times
        
    3 Answers
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
- 
                    1I'm not sure how to add text files in the same directory, this is why I asked. Sorry for the confusion, how do you do this on a macbook? Thanks – Ayaet Rakem Nov 17 '20 at 02:21
- 
                    Oh, try copying the file and then pasting it into the desired directory. – Epic Gamer Nov 17 '20 at 02:24
 
    