0

I've written a program using Python 3.10.5 in Visual Studio to read in a block of text from a file and format it as a list. Pretty simple, saves the list to the clipboard so that it can be pasted straight away. Except, when it encounters certain characters in the block of text, errors appear:

  • a colon (:) causes the program to treat the following lines like a command. The command cannot be understood, so an error occurs.
  • a less than symbol (<) causes the program to look for a file, declare the file cannot be found, and throw an error.
  • a more than symbol (>) causes the program to open a new, blank text file, named using the characters that followed the > symbol, and paste the remaining block of text into it.

Why is this happening? And how can I make it stop happening without manually specifying which characters must be ignored or removed from the text before parsing? Code attached below.

import os

def convert_syn_to_list_clip():
    out_list = []
    with open('syn_list.txt', 'r+', encoding = 'utf-8') as f1:
        for line in f1.readlines():
            out_list.append(line.rstrip('\n'))
        if len(out_list) == 0:
            print("EMPTY LIST !!!")
        f1.truncate(0)
    return str(out_list)

def write_to_clipboard(text):
    command = 'echo | set /p nul=' + text.strip() + '| clip'
    os.system(command)

write_to_clipboard(convert_syn_to_list_clip())
print("\n\nList in clipboard.\n\n")
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 2
    There are better ways to copy to the clipboard, see https://stackoverflow.com/q/579687/5987. Using an external command is always going to be a pain. – Mark Ransom Aug 02 '22 at 16:09
  • does `clip` support a file argument – rioV8 Aug 02 '22 at 16:10
  • what if you quote the string with double or single quotes, or escape the special charaters – rioV8 Aug 02 '22 at 16:12
  • @rioV8 even if you escape them you need to ensure you're using the proper character set. Not worth the hassle. – Mark Ransom Aug 02 '22 at 16:16
  • 1
    The Windows `command.exe` is known to be *weak* when it comes to escaping special characters. It was built to be simple not to be as powerful as a Unix shell, because MS/DOS had to be useable on machines having one single 360 ko floppy, 256 ko RAM and no hard disk. Long story short: you are taking a wrong path. Don't go that way. Just don't. – Serge Ballesta Aug 02 '22 at 16:23
  • @MarkRansom thanks for the comment. I tried this method first but the clipboard was coming up empty every time. I'll try again and see if I can make that work instead. – PublicMutiny Aug 03 '22 at 10:36
  • @rioV8 I thought about it, but not knowing how many characters have this effect (or what they are) would be a risk when I work with larger data sets. If I've had the code running for an hour then it hits a character that I haven't specified, it could mess up the entire output. Not worth the time and too much of a risk! – PublicMutiny Aug 03 '22 at 10:39
  • 1
    There are lots of answers at the link I gave, it's worth trying more than one. I can personally vouch for the one I wrote, I've been using it for years and it handles oddball characters without a problem. – Mark Ransom Aug 03 '22 at 11:46

0 Answers0