1

I have a .fhx file that I could open normally with notepad but I want to open it using Python. I have tried subprocess.popen which I got online but I keep getting errors. I also want to be able to read the contents of this file like a normal text file like how we do in f=open("blah.txt", "r") and f.read(). Could anyone guide me in the right direction ?

    import subprocess
    filepath = "C:\Users\Ch\Desktop\FHX\fddd.fhx"
    notePath = r'C:\Windows\System32\notepad.exe'
    subprocess.Popen("%s %s" % (notePath, filepath))
Cheth_K
  • 57
  • 8

3 Answers3

1

Solved my problem by adding encoding="utf16" to the file open command.

    count = 1
    filename = r'C:\Users\Ch\Desktop\FHX\27-ESDC_CM02-2.fhx'
    f = open(filename, "r", encoding="utf16") #Does not work without encoding
    lines = f.read().splitlines()
    for line in lines:
        if "WIRE SOURCE" in line:
            liner = line.split()
            if any('SOURCE="INPUT' in s for s in liner):
                print(str(count)+") ", "SERIAL INPUT = ", liner[2].replace("DESTINATION=", ""))
            count += 1

Now I'm able to get the data the way I wanted.Thanks everyone.

Cheth_K
  • 57
  • 8
  • the code can't possibly work as it is shown: `open()` doesn't support `encoding` parameter in Python 2; you might have mean `io.open()`. And `'C:\Users'` is a `SyntaxError` on Python 3; you should use raw string literals for Windows paths: `r'C:\Users'`. – jfs Jul 02 '15 at 10:51
  • Also, no need to call `.read().splitlines()`; a file is an iterator over lines in Python: `for line in f` works fine. – jfs Jul 02 '15 at 10:53
  • I'm using python 3. Sorry I forgot to mention it. This code works fine for me.I'm getting the paths from tkinter askopenfilename now. Also when i replace lines as f inside the (for line in f:) I get " UnicodeError: UTF-16 stream does not start with BOM". I'm new to python and don't understand unicode that well yet so I'm mostly going by trial and error. How do I resolve this error ? – Cheth_K Jul 02 '15 at 11:21
  • cargo cult programming can be harmful. You should *learn* enough to *understand* basics instead. If your code had worked previously with `.read().splitlines()` then it should continue to work if you remove it and use `for line in f` instead. If the file has no BOM then use `utf-16le` or `utf-16be` encodings. Ask a separate SO question if it fails. – jfs Jul 02 '15 at 11:49
0

try with shell=True argument subprocess.call((notePath, filepath), shell=True )

aptro
  • 51
  • 3
  • Awesome it opens thank you but I also want to read the contents of the file like how we read a text file for eg: f=open("asa.txt","r") and f.read(). Also I want to hide the pop up window of the notepad file. – Cheth_K Jul 01 '15 at 11:07
  • then it doesnot required to use subprocess. As you can simply f = open("asa.txt","r") for reading line f.readline() you can go to the python docs for complete reference – aptro Jul 01 '15 at 11:10
  • As i can simply ? The file I'm working with is a .fhx file it doesnt open and read like a normal text file by using the open("sss.fhx","r") command – Cheth_K Jul 01 '15 at 11:13
  • Any file can be opened and read with open(), even binary files. – Rob Foley Jul 01 '15 at 11:18
  • I need to read the strings in that file so if I use this "rb" it gives a str error – Cheth_K Jul 01 '15 at 11:28
-1

You should be passing a list of args:

import subprocess
filepath = r"C:\Users\Ch\Desktop\FHX\fddd.fhx"
notePath = r'C:\Windows\System32\notepad.exe'
subprocess.check_call([notePath, filepath])

If you want to read the contents then just open the file using open:

with open(r"C:\Users\Ch\Desktop\FHX\fddd.fhx") as f:
     for line in f:
        print(line)

You need to use raw string for the path also to escape the f n your file path name, if you don't you are going to get errors.

In [1]: "C:\Users\Ch\Desktop\FHX\fddd.fhx"
Out[1]: 'C:\\Users\\Ch\\Desktop\\FHX\x0cddd.fhx'

In [2]: r"C:\Users\Ch\Desktop\FHX\fddd.fhx"
Out[2]: 'C:\\Users\\Ch\\Desktop\\FHX\\fddd.fhx'
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I get <_io.TextIOWrapper name='C:\\Users\\Ch\\Desktop\\FHX\\27-ESDC_CM02-2.fhx' mode='r' encoding='cp1252'> as the output it's not what I want. You see in this fhx file when i convert it to .txt there are some strings in it that I need to find to do some operations so for that I want to read this fhx file like how I'd read a txt file. – Cheth_K Jul 01 '15 at 11:26
  • Typo, should have been line – Padraic Cunningham Jul 01 '15 at 11:28
  • Hi thank you I can now output the file but it still isnt like a textfile. Before I used this > lines = f.read().splitlines() for line in lines: if "WIRE SOURCE" in line: to find "wire source" but now I cant. Could you tell me how I can do that too ? – Cheth_K Jul 01 '15 at 12:29
  • what does the output look like? – Padraic Cunningham Jul 01 '15 at 12:35
  • The output looks like how it would look if I read a textfile and printed it out but there are some spaces in it for eg: if i used the text file then WIRED SOURCE would come but now it comes as W I R E D S O U R C E.(There are 3 spaces between wired and source it doesnt appear here in the comments)It looks the same but its different in format.The actual output is too large to put in the comments. I just need to find this particular word – Cheth_K Jul 01 '15 at 12:40
  • use `print(line, end='')` to avoid doubling newlines. – jfs Jul 01 '15 at 16:20
  • It doesn't change the previous output. If i do print(line.split()) then I get this as output ['ÿþ/\x00*\x00', '\x00V\x00e\x00r\x00s\x00i\x00o\x00n\x00:\x00', '\x001\x001\x00.\x003\x00.\x001\x00.\x004\x002\x005\x006\x00.\x00x\x00r\x00', '\x00*\x00/\x00'] but if I just print(line) then it appears like a text file but with spaces. I'm guessing this the reason why when I search for the string WIRE SOURCE line by line it doesnt work how do I make sense of the line.split() output ? – Cheth_K Jul 01 '15 at 21:44
  • what does `print(repr(line))` output? – Padraic Cunningham Jul 01 '15 at 21:48
  • line.split outputs what looks like a comment, you can see exactly what it is with `print("".join(l)) -> ÿþ/*Version:11.3.1.4256.xr*/ ` – Padraic Cunningham Jul 01 '15 at 21:50
  • Yeah it comes as /* V e r s i o n just like the rest of the text when I output it (there are spaces in between the letters). So now if I want to search for version in the rest of the file say line by line how do I do it ? If "V e r s i o n" in line: returns nothing for me. – Cheth_K Jul 02 '15 at 06:33