How can I fix error: 'expected str, bytes or os.PathLike object, not _io.TextIOWrapper' in Python FTP

How can I fix error: 'expected str, bytes or os.PathLike object, not _io.TextIOWrapper' in Python FTP

我有以下代码。这段代码所做的只是从用户的电脑上获取一些项目,然后将它们上传到网站。

# log in
session = ftplib.FTP('host', 'username', 'password')

# upload webpage
try:
    print(Fore.LIGHTMAGENTA_EX + "What's the name of the movie? ", end='')
    print(Fore.WHITE + '', end='')
    name = input('')
    session.cwd(dirname='../video')
    htmlfilecontent = '''<!DOCTYPE html>
<html lang="en">

<head>
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4MRZ3QHNFE"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag() { dataLayer.push(arguments); }
        gtag('js', new Date());

        gtag('config', 'G-4MRZ3QHNFE');
    </script>
    <title>Watching A Movie</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Indie+Flower&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../css/stylesheet.css">
    <link rel="icon" type="image/png" href="../logo.png" />
    <meta name="description" content="Watch new movies for free on our platform!" />
    <meta name="keywords"
        content="film, movies, free movies, full movies, Quessts, free full movie, free streaming, online movie" />
    <meta name="author" content="Quessts">
</head>

<body style="background-color: #303030; font-size: x-large;">
    <nav class="navbar navbar-expand-lg navbar-light bg-dark">
        <div class="container-fluid">
            <a href="../index.html">
                <img src="../logo.png" height="80px" width="80px">
            </a>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link active" href="../index.html"
                            style="color: aliceblue; margin-left: 20px; margin-right: 20px;">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="../request.html"
                            style="color: aliceblue; margin-left: 20px; margin-right: 20px;">Request</a>
                    </li>
                    <li class="nav-item ">
                        <a class="nav-link active" href="../contact.html"
                            style="color: aliceblue; margin-left: 20px; margin-right: 20px;">Contact</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="../about.html"
                            style="color: aliceblue; margin-left: 20px; margin-right: 20px;">About</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <br>
    <br>
    <br>
    <br>

    <h1 class="sameline" style="color: #AA0000;">''' + name + '''</h1>
    <br>
    <div class="sameline">

        <video width="800" height="400" controls>
            <source src="../videosrecords/''' + videoname + '''" type="video/mp4" />
        </video>
    </div>
    <br>
    <br>
    <br>
    <footer class="site-footer bg-dark">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-6">
                    <h6>About</h6>
                    <p class="text-justify">AnonFilms is a free video file sharing website dedicated to all users. This
                        website
                        contains movies from all genres and exists for the sole purpose of user entertainment.
                    </p>
                </div>

                <div class="col-xs-6 col-md-3">
                    <h6>Legal</h6>
                    <ul class="footer-links">
                        <li><a href="../TermsOfService.html">Terms of service</a></li>
                        <li><a href="../PrivacyPolicy.html">Privacy Policy</a></li>
                    </ul>
                </div>

                <div class="col-xs-6 col-md-3">
                    <h6>Quick Links</h6>
                    <ul class="footer-links">
                        <li><a href="../index.html">Home page</a></li>
                        <li><a href="../request.html">Request a movie</a></li>
                        <li><a href="../contact.html">Contact us</a></li>
                        <li><a href="../about.html">About us</a></li>
                    </ul>
                </div>
            </div>
            <hr>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-sm-6 col-xs-12">
                    <p class="copyright-text">Copyright &copy; 2020 All Rights Reserved by
                        <a href="../index.html">AnonFilms</a>.
                    </p>
                </div>
            </div>
        </div>
    </footer>
</body>
    '''
    realname = name.replace(" ", "")

    htmlfile = open(f'{realname}.html', 'w')
    htmlfile.write(htmlfilecontent)
    htmlfile.close()
    file = open(htmlfile, 'rb')
    session.storbinary(f'STOR {htmlfile}', file)
    print(Fore.GREEN + f"Successfully uploaded '{name}' to website")
except Exception as e:
    print(e)

出于某种原因,用户输入姓名后出现以下错误:

expected str, bytes or os.PathLike object, not _io.TextIOWrapper

这是我第一次遇到这个错误。这是什么意思?我该如何解决? ..................................................... ..................................................... ..................................................... .....................................

应该是这一行:

file = open(htmlfile, 'rb')

当它期望 strbytes 时,您将 file/_io.TextIOWrapper 作为第一个参数传递,如错误所示.

我猜这是你想要做的:

file = open(f"{realname}.html", 'rb')
session.storbinary(f'STOR {realname}.html', file)