Hi all, Ive been working on this script for a while, but, when I try to send it I get an IO error because, from what I see, its not saving the "temp.jpg" file. Not so important, the save function has the same problem. All the ports are okay, And I can send anything else with it, just can't save it, I cant save anything in the script for that matter, seems like something is wrong with the open() function because using open("temp.jpg", 'w') should make a new file in my python directory called temp.jpg, same with the save function. I am just wondering why it wont work. I can use open just fine in other scripts, so It has to be my code, but I can't find the problem, so I'm hoping you guys can. Oh, this might help: Im using hamachi for the connection address, but I don't think that is the problem cause I use hamachi for all my other socket scripts and it works just fine.

from Tkinter import *
from PIL import Image, ImageTk
import os, sys
import glob
from socket import *
import thread
import time
root=Tk()
global path
global imgN
global imgPaths
global maxFile
globals()['imgN'] = 0
globals()['imgPaths'] = ""
globals()['maxFile'] = -1
Host=''
Port=80
global SendingAddr
global ImgToSend
global exitT
globals()['ImgToSend']=""

def SendTo():
    def ret(event):
        Port1=80
        sSock=socket(AF_INET, SOCK_STREAM)
        sSock.connect((addr.get(), Port1))
        sSock.send(globals()['ImgToSend'])
        sSock.close()
        top.destroy()
    top=Toplevel()
    top.title("Send image to")
    addr=Entry(top,width=20)
    addr.grid(row=1,column=1)
    addr.bind("<Return>", ret)
def Recv(x,y):
    while 1:
        sSockR=socket(AF_INET, SOCK_STREAM)
        sSockR.bind((Host,Port))
        sSockR.listen(3)
        while 1:
            conn,addr=sSockR.accept()
            data = conn.recv(2048)
            if not data:
                conn.close()
            elif data:
                f=open("temp.jpg", 'wb')
                f.write(data)
                f.close()
                s=scale.get()
                s=s.strip()
                s=s.split(',')
                s[0] = int(s[0])
                s[1] = int(s[1])
                s=tuple(s)
                getImg("temp.jpg", s)
        conn.close()
    sSockR.close()
def exitprog():
    root.quit()
    sys.exit()
def getImg(path, size):
    try:
        fi=open(path, 'rb')
        globals()['ImgToSend']=fi.read()
        fi.close()
        img=Image.open(path)
        img=img.resize(size)
        photo=ImageTk.PhotoImage(img)
        imgl=Label(root, image=photo)
        imgl.image=photo
        imgl.grid(column=1, row=2, columnspan=3)
    except IOError:
        globals()['imgN'] = 0
        p=globals()['imgPaths']
        s=scale.get()
        s=s.strip()
        s=s.split(',')
        s[0] = int(s[0])
        s[1] = int(s[1])
        s=tuple(s)
        getImg(p[0],s)
def getp():
    globals()['Imgpath'] = dir01.get()
    GetList()
def GetList():
    exts=['.bmp','.dib',<very long list of image extensions>]
    p=globals()['Imgpath']
    os.chdir(p)
    globals()['imgPaths'] = glob.glob("%s/*.*" % p)
    temp = globals()['imgPaths']
    for x in temp:
        if '.'+x.split('.')[-1] not in exts:
            temp.remove(x)
    for x in temp:
        globals()['maxFile'] += 1
    globals()['imgPaths'] = temp
    globals()['imgN'] = 0
def next():
    curr=globals()['imgN']
    imgl=globals()['imgPaths']
    if curr > globals()['maxFile']:
        globals()['imgN'] = 0
        curr=0
    imgDir=imgl[curr]
    s=scale.get()
    s=s.strip()
    s=s.split(',')
    s[0] = int(s[0])
    s[1] = int(s[1])
    s=tuple(s)
    getImg(imgDir,s)
    globals()['imgN'] += 1
def prev():
    curr=globals()['imgN']
    imgl=globals()['imgPaths']
    curr -= 1
    if curr < 0:
        curr=1
        globals()['imgN'] = 1
    imgDir=imgl[curr]
    s=scale.get()
    s=s.strip()
    s=s.split(',')
    s[0] = int(s[0])
    s[1] = int(s[1])
    s=tuple(s)
    getImg(imgDir,s)
    globals()['imgN'] -= 1
def recvm():
    thread.start_new_thread(Recv, ("xx", "yy"))
def saveImg():
    def ret1(event):
        path="%s" % p.get()
        f=open(path, 'wb')
        imdata=globals()['ImgToSend']
        f.write(imdata)
        f.close()
        top1.destroy()
    top1=Toplevel()
    top1.title("Save image as")
    p=Entry(top1,width=20)
    p.grid(row=1,column=1)
    p.bind("<Return>", ret1)

m=Menu(root)
fm = Menu(m, tearoff=0)
fm.add_command(label="Save", command=saveImg)
fm.add_command(label="Send", command=SendTo)
fm.add_command(label="RecvMode", command=recvm)
fm.add_separator()
fm.add_command(label="Quit", command=exitprog)
m.add_cascade(label="File", menu=fm)
scale=Entry(root)
scale.grid(column=1, row=1)
scale.insert(0, "size x, size y")
next1=Button(root, text="Next", command=next)
next1.grid(column=3, row=1)
prev1=Button(root, text="Prev", command=prev)
prev1.grid(column=2, row=1)
dir01 = Entry(root)
dir01.insert(0, "Enter a directory")
dir01.grid(column=1, row=3)
ok=Button(root, text="Ok", command=getp)
ok.grid(column=2, row=3, columnspan=2)

root.config(menu=m)
root.mainloop()

If your open() call is "not working" perhaps you are mistaken with which working directory you're using. Try a simple os.getcwd() call to see where the files are being created.

Thank jlm699, I found my problem, I'm saving the the directory that I have them enter, that is an easy fix so, yey.

Cheers,
Micro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.