Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python代码中的错误(TypeError:强制使用Unicode:需要字符串或缓冲区,找到文件)_Python_Python 3.x - Fatal编程技术网

python代码中的错误(TypeError:强制使用Unicode:需要字符串或缓冲区,找到文件)

python代码中的错误(TypeError:强制使用Unicode:需要字符串或缓冲区,找到文件),python,python-3.x,Python,Python 3.x,错误发生在第49行“FileSizereMainingBytes=os.path.getsize(infle)” infle包含我要获取大小的文件。根据我在python文档中的理解,这应该是正确的。有人能告诉我出了什么问题吗 import sys, os buffer = 1000 try: #open file in binary mode for reading inFile = open(sys.argv[1],"rb") print "file name is: ",

错误发生在第49行“FileSizereMainingBytes=os.path.getsize(infle)” infle包含我要获取大小的文件。根据我在python文档中的理解,这应该是正确的。有人能告诉我出了什么问题吗

import sys, os

buffer = 1000

try:
#open file in binary mode for reading
    inFile = open(sys.argv[1],"rb")
    print "file name is: ", inFile.name

except IOError:
#check for IOExceptions
    print "Eror opening file"
    sys.exit()

else:
#create new directory for copying, create out file in new directory
    if (os.path.isdir("recv")):
            os.chdir("recv")
            try:
                    outFile = open(inFile.name,"wb")
            except IOError:
                    print "something went wrong creating the out file"
                    sys.exit()
    else :
            os.mkdir("recv")
            os.chdir("recv")
            try:
                    outFile = open(inFile.name,"wb")
            except IOError:
                    print "something went wrong creating the out file"
                    sys.exit()

#loop to copy bytes to new directory
    fileSizeRemainingInBytes = os.path.getsize(inFile)
    print "Initial size: ", fileSizeRemainingInBytes
    while fileSizeRemainingInBytes > 0 :
            print fileSizeRemainingInBytes
            bytesToCopy = inFile.read(buffer);
            outFile.write(bytesToCopy);
    inFile.close()

os.path.getsize
将文件路径作为参数,而不是文件对象。所以您实际上想要调用
os.path.getsize(infle.name)
。请注意,这不会给出要复制的剩余字节数;每次评估时,它只会给出整个文件的大小。要获得剩余的字节数,您必须跟踪读取的总字节数,并从文件大小中减去该总字节数

像这样的方法应该会奏效:

import sys
import os

buffer = 1000

with open(sys.argv[1], "rb") as in_file:

    # Make your `recv` directory as a sub-directory
    # or your current directory if it doesn't already exist
    if not os.path.isdir("recv"):
        os.mkdir("recv")


    # Create the path to the file to which you 
    # want to copy. When opened, you'll have a file
    # with the same file name as your input file,
    # but it will be in your `recv` subdirectory
    out_file_path = os.path.join("recv", in_file.name)

    # Read the bytes
    with open(out_file_path, "wb") as out_file:
        bytes_read = 0 
        bytes_to_read = os.path.getsize(in_file.name)
        while bytes_read < bytes_to_read:
            out_file.write(in_file.read(buffer))
            bytes_read += min(buffer, bytes_to_read - bytes_read)
            print "{} / {} bytes copied".format(bytes_read, bytes_to_read)
导入系统 导入操作系统 缓冲区=1000 打开时(sys.argv[1],“rb”)如_文件中所示: #将“recv”目录作为子目录 #或者您当前的目录(如果它不存在) 如果不是os.path.isdir(“recv”): os.mkdir(“recv”) #创建要访问的文件的路径 #我想复制。打开后,您将拥有一个文件 #使用与输入文件相同的文件名, #但它将在您的'recv'子目录中 out\u file\u path=os.path.join(“recv”,in\u file.name) #读取字节 打开(输出文件路径,“wb”)作为输出文件: 字节\u读取=0 字节到读取=os.path.getsize(在文件名中) 当字节读取<字节读取: out_file.write(in_file.read(缓冲区)) 字节读取+=min(缓冲区,字节读取-字节读取) 打印“{}/{}字节复制”。格式(字节读取,字节读取)
这是我运行它时的完整输出:%python copy.py test.txt文件名是:test.txt Traceback(最近一次调用):filesizeMainingBytes=os.path.getsize(infle)文件/usr/lib64/python2.6/genericpath.py,第49行,在getsize return os.stat(文件名)中.st_size TypeError:强制使用Unicode:需要字符串或缓冲区,文件找到谢谢,这解决了问题。然而,现在它只返回0的大小,即使我在一个相当大的jpeg文件上尝试了它。你知道为什么吗?没关系,我让它工作了。我将设置FileSizereMainingBytes移动到了我设置内嵌下的右上角。我不明白为什么会这样,尽管这是因为你更改了目录,然后用相同的名称创建了一个新的空文件。所以您得到了您创建的新文件的大小。