Skip to content

convert_to_csv.py does not work under windwos due to ":" in the csv paths #39

Open
@mr15

Description

@mr15

When running this script under Windows, I get the following error:

D:\Dateien\Dokumente\pyEmonCmsDownload>python.exe -m convert_to_csv.py
Converting phpfina feed 104076
Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 112, in _get_module_details
  File "D:\Dateien\Dokumente\pyEmonCmsDownload\convert_to_csv.py", line 57, in <module>
    phpfina_convert(username+"/phpfina/",username+"/csv/",f['id'],f['tag'],f['name'])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Dateien\Dokumente\pyEmonCmsDownload\convert_to_csv.py", line 22, in phpfina_convert
    csv_fh = open(csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv","w")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: 'niclas/csv/104076-Node:0-node:0:Z1_Temp01-T_DHW_oben.csv'

I think this is due to windows not allowing ":" in file names.

My proposal for a fix: Replace ":" with "-". Here is what this could look like (worked for me), essentially adding those two lines:

csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
csv_path = csv_path.replace(":", "-")

Resulting in:

def phpfina_convert(datadir,csvdir,feedid,tag,name):
    meta = phpfina_get_meta(datadir,feedid)
    print(tag)
    print(name)
    csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
    csv_path= csv_path.replace(":", "-")
    csv_fh = open(csv_path,"w")
    fh = open(datadir+str(feedid)+".dat","rb")
    for i in range(0,meta['npoints']):
        time = meta['start_time'] + i*meta['interval']
        val = struct.unpack("f",fh.read(4))
        csv_fh.write(str(time)+","+str(val[0])+"\n")
    fh.close()
    csv_fh.close()
    
def phptimeseries_convert(datadir,csvdir,feedid,tag,name):
    if os.path.isfile(datadir+"feed_"+str(feedid)+".MYD"):
        bytesize = os.stat(datadir+"feed_"+str(feedid)+".MYD").st_size
        npoints = int(bytesize/9.0)
        csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
        csv_path = csv_path.replace(":", "-")
        csv_fh = open(csv_path,"w")
        fh = open(datadir+"feed_"+str(feedid)+".MYD","rb")
        
        for i in range(0,npoints):
            tmp = struct.unpack('<xIf',fh.read(9))
            csv_fh.write(str(tmp[0])+","+str(tmp[1])+"\n")
        fh.close()
        csv_fh.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions