未找到指定的 WinError2 文件 (Python 3.9)

Posted

技术标签:

【中文标题】未找到指定的 WinError2 文件 (Python 3.9)【英文标题】:WinError2 File Specified Not Found (Python 3.9) 【发布时间】:2021-05-15 12:36:55 【问题描述】:

我对python了解不多,需要做一个UDP P2P文件共享app/code。以前从未使用过python,所以请原谅我缺乏知识。我需要帮助来解决此错误:

    `FileNotFoundError: [WinError 2] The system cannot find the file specified`
   

这是我的 server.py 代码:

import os
import socket
import time

host = input("Host Name: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


try:
    sock.connect((host, 22222))
    print("Connected Successfully")
except:
    print("Unable to connect")
    exit(0)


file_name = sock.recv(100).decode()
file_size = sock.recv(100).decode()


with open("./rec/" + file_name, "wb") as file:
    c = 0
  
    start_time = time.time()

    
    while c <= int(file_size):
        data = sock.recv(1024)
        if not (data):
            break
        file.write(data)
        c += len(data)

   
    end_time = time.time()

print("File transfer Complete. Total time: ", end_time - start_time)


sock.close()

此代码由多个网站和github组成,如果您看到任何抄袭,请见谅。

client.py:

import os
import socket
import time


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostname(), 22222))
sock.listen(5)
print("Host Name: ", sock.getsockname())


client, addr = sock.accept()


file_name = input("File Name:")
file_size = os.path.getsize(file_name)


client.send(file_name.encode())
client.send(str(file_size).encode())


with open(file_name, "rb") as file:
    c = 0
   
    start_time = time.time()

   
    while c <= file_size:
        data = file.read(1024)
        if not (data):
            break
        client.sendall(data)
        c += len(data)

    
    end_time = time.time()

print("File Transfer Complete. Total time: ", end_time - start_time)

sock.close()


      ```

【问题讨论】:

您为server.pyclient.py 发布了相同的代码 哈哈哈我的错。谢谢,修好了。 请编辑您的问题以包含异常的完整回溯。这应该可以告诉您问题出在哪里。 【参考方案1】:

您遇到的错误消息是由于找不到文件。最有可能是报告错误的client.py 文件。在尝试打开它之前,您应该检查文件是否存在。

file_name = input("File Name: ")
# Ensure the file exists
if not os.path.isfile(file_name):
    print(f"File not found: file_name")
    exit(0)

由于您从不同来源复制和粘贴,代码中存在许多错误。下面的代码应该适合你,我已经添加了 cmets 来解释它是如何工作的。

如果有什么需要进一步解释的,请在 cmets 中告诉我。

server.py

import os
import socket
import time

SERVER_HOST = "0.0.0.0"
SERVER_PORT = 22222
BUFFER_SIZE = 1024 # Receive 1024 bytes at a time (1kb)
SEPARATOR = ":::::"

SERVER_FOLDER = "./rec/" # Location to save file

# Create the server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the host address and port
sock.bind((SERVER_HOST, SERVER_PORT))

# Enable server to accept connections
sock.listen()
print(f"Server listening SERVER_HOST:SERVER_PORT")

# Accept client connection
client_socket, address = sock.accept()
print(f"Client address connected.")

# Receive the file information
received = client_socket.recv(BUFFER_SIZE).decode()

# Split the string using the separator
file_name, file_size = received.split(SEPARATOR)

print(f"File name: 'file_name'")
print(f"File size: 'file_size'")

# Set the path where the file is to be saved
save_path = os.path.join(SERVER_FOLDER, file_name)

with open(save_path, "wb") as file:
    bytes_received = 0
    start_time = time.time()
    while True:
        data = client_socket.recv(BUFFER_SIZE)
        if not (data):
            break
        file.write(data)
        bytes_received += len(data)
    end_time = time.time()

print(f"File transfer Complete. bytes_received of file_size received. Total time: end_time - start_time")

# Close the client socket
client_socket.close()

# Close the server socket
sock.close()

client.py

import os
import socket
import time

SERVER_PORT = 22222
BUFFER_SIZE = 1024 # Send 1024 bytes at a time (1kb)
SEPARATOR = ":::::"

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()

print(f"Connecting to host:SERVER_PORT")
sock.connect((host, SERVER_PORT))
print("Connected.")

file_name = input("File Name: ")

# Ensure the file exists
if not os.path.isfile(file_name):
    print(f"File not found: file_name")
    exit(0)

# Get the file size
file_size = os.path.getsize(file_name)

# Send file_name and file_size as a single string
sock.send(f"file_nameSEPARATORfile_size".encode())

with open(file_name, "rb") as file:
    c = 0
    start_time = time.time()
    while c <= file_size:
        data = file.read(BUFFER_SIZE)
        if not (data):
            break
        sock.sendall(data)
        c += len(data)
    end_time = time.time()

print("File Transfer Complete. Total time: ", end_time - start_time)

sock.close()

【讨论】:

根据你的代码。我应该如何运行它。我试过并输入了我的文件名,但“./rec/”是我不明白的。它有什么作用?收到找不到“./rec/”目录的错误。重复说我对python一无所知。谢谢。 "./rec/" 将是服务器文件所在位置中名为“rec”的目录。我直接从您在问题中提供的代码中获取了它。

以上是关于未找到指定的 WinError2 文件 (Python 3.9)的主要内容,如果未能解决你的问题,请参考以下文章

PythonPDF: FileNotFoundError: [WinError 2] 系统找不到指定的文件

midi2audio/FluidSynth: [WinError 2] 系统找不到指定的文件

pydot.InvocationException:未找到 GraphViz 的可执行文件

Azure 函数 - “未找到任何已初始化的语言工作者”

未找到指定的sqlserver

未找到匹配的配置文件(所有有效的配置文件都不允许指定的权利)