1
import fileinput, sys, codecs, re, unicodedata

def remove_control_characters(s):
    return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")


file_in = 'file_with_ctrl_characters.XML'
file_out = 'out_file.xml'

with open(file_out, 'a') as out:
    for line in fileinput.input([file_in]):
        out.write(remove_control_characters(line)+'\n')
out.close()
os.remove(file_in)
os.rename('out_file.xml', file_in)

In short, this code works in jupyter notebook. It read a file, removes special character and then writes everything else to a new xml file.

Then it removes the old file, and gives the old file name to the new file. I'm left with same file name but without the special character.

I want to call this from command prompt passing it (one?) argument - the path of the file I need it to remove special character from.

How do I go from Jupyter notebook code to a script I can call on from command prompt by providing it the file to remove characters from?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
fafz
  • 81
  • 7
  • Does this answer your question? [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – mkrieger1 Jan 30 '20 at 20:05
  • Did you even try to search for something like "python command prompt argument"? – mkrieger1 Jan 30 '20 at 20:06
  • You are already using the `fileinput` module which should serve the purpose you describe. What is the problem with it? – mkrieger1 Jan 30 '20 at 20:08
  • The problem is that I can't figure out how to call this from command prompt by giving it my own file's path/name. Yes, I have read several examples but have not had success making it work with my code. – fafz Jan 30 '20 at 20:12
  • Did you try `python – mkrieger1 Jan 30 '20 at 20:13
  • So I saved this code in notepad as cc_cleaner.py file. I open cmd from the folder where everything is placed and I run "python cc_cleaner.py "NAK - Copy.XML". NAK - Copy.XML is the file I'm trying to remove characters from. I also tried python3 instead of python. Nothing happens – fafz Jan 30 '20 at 20:18
  • Did you still use the hard coded file name `'file_with_ctrl_characters.XML'` in the script? Obviously you need to change that. – mkrieger1 Jan 30 '20 at 20:25
  • If I change it to the file I want to run this script on, that defeats the purpose. I want to be able to pass the file name from command prompt. I can remove it from the code but I'm still trying to figure out how to be able to pass in that value for file name/path. – fafz Jan 30 '20 at 20:32
  • You should change the code so that it *does not* use a hard-coded filename, but the filename given on the command line. Unfortunately I don't seem to be able to explain this to you sufficiently, so you should read a book or do a tutorial or find someone who can teach you this in person. – mkrieger1 Jan 30 '20 at 20:34
  • You're absolutely correct and that's exactly what I need help with and asked in my initial post. – fafz Jan 30 '20 at 20:37

0 Answers0