0

hi guys how are you I hope that well, I'm new using python and I'm doing a program but I dont know how to save the data permanently in a file. I only know how to create the file but i dont know how can i keep the data on the file eventhough the program be closed and when i open it back i be able to add more data and keep it in the file too.I have also tried several methods to upload the file on python but they didnt work for me. Can someone please help me? This is my code:

file = open ('file.txt','w')

t = input ('name :')
p= input ('last name: ')
c = input ('nickname: ')
file.write('name :')
file.write(t)
file.write('  ')
file.write('last name: ')
file.write(p)
file.write('nickname: ')
file.write(c)
file.close()

with open('archivo.txt','w') as file:
data = load(file)
print(data)
uli28
  • 23
  • 4
  • 1
    You can open the file in append mode 'a', or 'r+' if you want to read the old data first. There's a good summary of the different modes [here](https://stackoverflow.com/a/23566951/4014959). – PM 2Ring Aug 01 '18 at 06:10
  • 1
    Read this: https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python – Humbalan Aug 01 '18 at 06:11
  • 1
    The info at that pythonforbeginners link has some problems. The code isn't indented correctly, and it's focused on Python 2. If you're just starting with Python now you really should be learning Python 3, not Python2! The [official tutorial](https://stackoverflow.com/a/23566951/4014959) is much better. I guess it might be a bit too technical for someone new to programming, but you should definitely take a look at it. – PM 2Ring Aug 01 '18 at 06:18
  • I advise that you forget about uploading or downloading files for a little while. Learn the basics of Python first before you try to do that more advanced stuff. It's not hard to upload or download, but it's easy to make a mess if you don't know what you're doing. ;) – PM 2Ring Aug 01 '18 at 06:24
  • 1
    Are you really using the old `python` 2.7 as you tagged? If so, then you should be using `raw_input()` instead of `input()`. Check first using `python -V` (uppercase `V`) on the command-line. – cdarke Aug 01 '18 at 06:31

2 Answers2

0

Here is a demonstration of how file writing works, and the difference between w and a. The comments represent the text in the file that is written to the drive at each given point.

f1 = open('appending.txt', 'w')
f1.write('first string\n')
f1.close()

# first string

f2 = open('appending.txt', 'a')
f2.write('second string\n')
f2.close()

# first string
# second string

f3 = open('appending.txt', 'w')
f3.write('third string\n')
f3.close()

# third string
Alistair Carscadden
  • 1,198
  • 5
  • 18
0

There are three type of File operation mode can happen on file like read, write and append.

  • Read Mode: In this you only able to read the file like

    #content in file.txt "Hi I am Python Developer"
    with open('file.txt', 'r') as f:
          data = f.read()
          print(data)
    #output as : Hi I am Python Developer
    
  • Write Mode: In this you are able to write information into files, but it will always overwrite the content of the file like for example.

    data = input('Enter string to insert into file:')
    with open('file.txt', 'w') as f:
         f.write(data)
    
    with open('file.txt', 'r') as f:
         data = f.read()
         print('out_data:', data)
    
    # Output : Enter string to insert into file: Hi, I am developer
    #          out_data: Hi, I am developer
    

When you open file for next time and do same write operation, it will overwrite whole information into file.

  • Append Mode: In this you will able to write into file but the contents is append into this files. Like for example:

    data = input('Enter string to insert into file:')
    with open('file.txt', 'a') as f:
         f.write(data)
    
    with open('file.txt', 'r') as f:
         data = f.read()
         print('out_data:', data)
    
    # Output : Enter string to insert into file: Hi, I am developer
    #          out_data: Hi, I am developer
    
    # Now perform same operation:
    data = input('Enter string to insert into file:')
    with open('file.txt', 'a') as f:
        f.write(data)
    
    with open('file.txt', 'r') as f:
        data = f.read()
        print('out_data:', data)
    # Output : Enter string to insert into file: Hi, I am Python developer
    #           out_data: Hi, I am developer Hi, I am Python Developer
    
utks009
  • 573
  • 4
  • 14
  • utks009 thank you so much it is so helpful for me i was confuse how to use the different types of modes – uli28 Aug 01 '18 at 06:42