2

I have a text file path stored in a variable called settings_data_path and I want to take whatever is in that text file and put it into a string variable, in this instance, limited_n_ints . So I opened the file and put it in a variable using

settings_data = open(settings_data_path, "r")
limited_n_ints = (settings_data.read())
print(limited_n_ints)
settings_data.close()

Whenever I open the data path with w+ and write something in the file, everything worked fine. However, when I print limited_n_ints, nothing shows up. Does anyone know how to store this file in a variable?

4
  • If this code prints nothing, that means the file was empty. Commented Dec 3, 2019 at 22:47
  • Above code is correct, it's printing the content of the text file for me Commented Dec 3, 2019 at 22:54
  • @JohnGordon the file is not empty, I checked it before trying to read it, and the file path is correct. Commented Dec 3, 2019 at 23:13
  • 1
    I stand by my statement. If that code does not raise an error and just prints nothing, then the file was empty. If you viewed the file in notepad and it had contents, then that wasn't the same file being opened in the code. Commented Dec 3, 2019 at 23:48

1 Answer 1

3

Try

setting_data = open('inventory.4.txt', 'r')
lines = setting_data.readlines()
limited_n_ints = ''
for i in lines:
  limited_n_ints = limited_n_ints + i
print(limited_n_ints)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.