0

I created a txt file named as directory path+current date and time. The following error occurs:

File cannot be opened. coercing to Unicode: need string or buffer, NoneType found

def create_file(count):
    filename = "countMetrics"
    dir = os.getcwd()

    #print 'Current directory path is-'
    #print dirPath  

    date = datetime.datetime.now()
    now = date.strftime("%Y-%m-%d %H:%M")
    #print 'current date and time is-'
    #print now

    ## date and time representation
    #print "Current date & time " + time.strftime("%c")
    dirPath = os.path.join(dir, filename)
    filenameCreated = dirPath+now+".txt"
    #filenameCreated = dirPath+filename+now+".txt"
    print filenameCreated
    f = openfile(filenameCreated,'a')
    return f
    #writeFile(f,count)


#defining openfunction
def openfile(filename,mode):
    try:
        open(filename,mode)
    except Exception, err:
        print("File cannot be opened.")
        print(str(err))
        return


def readFile(filename):  
    try:
        target = open(filename,'r')
        content=filename.read()     # reading contents of file
        for line in target:
            print content
        target.close()
    except:
        print "File is empty.."
        return


#defining write function    
def writeFile(filename,count):
    try:
        target = openfile(filename,'a')
        target.write(count)
        target.close()
    except Exception, err:
        print("File have no data to be written.")
        print(str(err))
        return
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
komal bhardwaj
  • 87
  • 2
  • 2
  • 7
  • try using "f = openfile(filenameCreated,'a+')" instead. 'a' will try to open an existing file to append data. 'a+' will do the same but it will create it if it does not exist. If you want to replace the existing file for a new one use 'w' – dgsleeps Feb 24 '15 at 06:37
  • FWIW, filenames containing `:` are not portable: it's [invalid in Windows](http://stackoverflow.com/q/10386344/4014959). – PM 2Ring Feb 24 '15 at 06:48
  • @dgsleeps 'a' mode will also create a file if it does not exist. – thiruvenkadam Feb 24 '15 at 06:52
  • how to remove : from the file name? time contains : – komal bhardwaj Feb 25 '15 at 06:26

1 Answers1

0

Your openfile function is not returning anything. Change it to return the open file descriptor and your code might work :-)

def openfile(filename, mode):
try:
    return open(filename, mode)
except Exception as err:
    print "File cannot be created", err
    return

And add an if in the main code to check whether you receive file descriptor.

f = openfile(filenameCreated,'a')
if not f:
    print "No file created"
    return
return f

And your writeFile function will be like this:

def writeFile(target, count):
    try:
        target.write(count)
        target.close()
        return 1
    except Exception as err:
        print "Cannot write into the file"
        return 0

Because your openfile itself returns a descriptor. You don't need to create another one.

thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26