3

I was doing a project in which I had to make a multiclipboard. Here's what it'll do:

This multiclipboard would run through the Terminal. It will create a file named clipboardd and save all the copied texts over there. The person can add as many copied texts as he wants and he also can clear the multiclipboard if he wants to.

Here's the code:

import pyperclip
import sys

jim=open('multiclipboardd','w')

#This will copy text to the multiclipboard
if len(sys.argv)==2 and (sys.argv[1].lower())=='save':
    jim=open('multiclipboardd','a')
    jim.write(pyperclip.paste())
    jim.write('\n')
    print('The text has been pasted to the multiclipboard!')
    jim.close()


#This will read text from the multiclipboard
elif len(sys.argv)==2 and (sys.argv[1].lower())=='list':
    kk=open('multiclipboardd')
    print(kk.read())


#This will delete the text of the multiclipboard
elif len(sys.argv)==2 and (sys.argv[1].lower())=='delete':
    jim=open('multiclipboardd','w')
    jim.write('')
    print('The clipboard has been cleared!')
#jim and kk are just variables

The name of this file is Panda.py

Calling python panda.py save in the Terminal should save the curent copied text to a folder named clipboardd and it does! This works perfectly fine when I try to call it.

However, when I try to run python panda.py list in the Terminal, it is expected that it would print al the copied words on the screen but it deletes them all! Suppose that before calling python panda.py list, clipboardd has 110 letters. Then after calling python panda.py list, it has 0 letters!

Why is read() deleting all the characters inside the file clipboardd?

M.Hamel
  • 375
  • 1
  • 3
  • 10
  • 2
    `read()` isn't truncating your file. The unconditional `jim=open('multiclipboardd','w')` at the top of your file is. – Amadan Aug 17 '17 at 02:24
  • `jim=open('multiclipboardd','w')` is expected to remove the data! Moreover, Python will reach `jim=open('multiclipboardd','w')` only when a person calls `python panda.py delete` – M.Hamel Aug 17 '17 at 02:26
  • What is the third line of your code? Right after the two `import` statements? – Amadan Aug 17 '17 at 02:26
  • @Amadan Damn!!! You're absolutely correct. Thanks for pointing out – M.Hamel Aug 17 '17 at 02:29

3 Answers3

6

Each time you open your file with 'w' mode, it overwrites all the existing data in the file. read() isn't doing this. To prevent this, open the file with 'a' mode everytime. Hope this helps.

Debanik Dawn
  • 797
  • 5
  • 28
5

When you do jim=open('multiclipboardd','w') at the top of your program, it truncates the original file and erases it. That's why you're file's getting erased.

Also, when you open files you should .close() them or use a context manager.

SH7890
  • 545
  • 2
  • 7
  • 20
  • I did think about this while writing the code. But do you think that it is really useful? We're running this in the Terminal! not in the IDLE. It works perfect without .close()! Anyway, thanks for the answer :) – M.Hamel Aug 17 '17 at 02:33
  • 1
    @M.Hamel Yeah, it works, but you avoid problems in the long run. In longer code or in multiple programs that access the same file, you avoid race conditions, and other I/O errors. It really is worth getting used to using `.close()` – SH7890 Aug 17 '17 at 02:35
  • @M.Hamel Also, running it in the terminal or IDLE or any IDE doesn't matter. file objects still should be closed. – SH7890 Aug 17 '17 at 12:26
1

As Amadan said, read() is not truncating your file.

The unconditional jim=open('multiclipboardd','w') at the top of your file is.

If you dont want it to delete your content, replace the 'w' with an 'a'

Nate
  • 481
  • 3
  • 11