怎样把txt导入python,导入外部“ .txt” python文件

I am trying to import a text with a list about 10 words.

import words.txt

That doesn't work...

Anyway, Can I import the file without this showing up?

Traceback (most recent call last):

File "D:/python/p1.py", line 9, in

import words.txt

ImportError: No module named 'words'

Any sort of help is appreciated.

解决方案

You can import modules but not text files. If you want to print the content do the following:

Open a text file for reading:

f = open('words.txt', 'r')

Store content in a variable:

content = f.read()

Print content of this file:

print(content)

After you're done close a file:

f.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的`ftplib`库来实现FTP文件传输协议的功能,从而实现与外部文件服务器的连接和文件导入。 以下是一个简单的示例代码,可以根据自己的需求进行修改: ```python import ftplib # 服务器地址、用户名、密码 host = 'ftp.example.com' username = 'your-username' password = 'your-password' # 连接服务器 ftp = ftplib.FTP(host) ftp.login(username, password) # 列出服务器上的文件列表 ftp.cwd('/') file_list = ftp.nlst() print("文件列表:", file_list) # 下载文件到本地 local_file_path = '/path/to/local/file' remote_file_path = '/path/to/remote/file' with open(local_file_path, 'wb') as f: ftp.retrbinary('RETR %s' % remote_file_path, f.write) # 关闭连接 ftp.quit() ``` 其中,`host`、`username`和`password`需要根据自己的实际情况进行修改。`ftp.cwd('/')`可以切换到服务器的根目录,`ftp.nlst()`可以列出该目录下的文件列表。`ftp.retrbinary('RETR %s' % remote_file_path, f.write)`可以下载远程文件到本地,其中`remote_file_path`为远程文件的路径,`f.write`为将文件内容写入到本地文件中。 需要注意的是,`ftplib`库只支持FTP协议,如果需要使用其他协议,可以考虑使用第三方库,如`sftp`库或`paramiko`库。 ### 回答2: 使用Python导入外部文件服务器是非常简单的。首先,我们需要使用Python的`requests`库来与外部服务器进行通信。然后,我们可以使用`requests.get()`函数来获取外部服务器上的文件。以下是一个简单的示例代码: ```python import requests def import_file_from_server(url, filename): response = requests.get(url) if response.status_code == 200: # 确保成功连接到服务器 with open(filename, 'wb') as file: file.write(response.content) # 将服务器上的文件内容写入本地文件 print('文件导入成功!') else: print('连接服务器失败。') # 使用示例 url = 'http://example.com/files/data.txt' # 外部文件服务器的URL filename = 'data.txt' # 要保存的本地文件名 import_file_from_server(url, filename) ``` 在这个例子中,`import_file_from_server()`函数接受外部文件服务器的URL和要保存的本地文件名作为参数。然后,`requests.get()`函数发送一个GET请求到服务器,并返回服务器的响应。如果响应状态码为200,表示连接成功,我们可以通过`response.content`属性访问该文件的内容,并将其写入本地文件中。 以上是一个简单的示例,你可以根据实际需求进行扩展和改进。 ### 回答3: 要使用Python编写一个可以导入外部文件服务器的程序,可以使用socket模块和ftplib模块。 首先,我们需要使用socket模块创建一个套接字,并连接到外部服务器。可以使用socket的connect方法来建立TCP连接,通过提供服务器的IP地址和端口号来连接。 接下来,我们可以使用ftplib模块来进行文件传输操作。使用ftplib的FTP类,我们可以登录到外部服务器并导入文件。可以使用FTP类的login方法提供服务器的用户名和密码来登录,然后使用FTP类的storbinary方法来上传文件。 以下是一个示例代码,演示如何导入一个名为file.txt文件外部服务器: ```python import socket import ftplib # 连接外部服务器 server_ip = "服务器IP地址" server_port = 服务器端口号 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((server_ip, server_port)) # 登录到服务器 ftp = ftplib.FTP() ftp.connect(server_ip) ftp.login("用户名", "密码") # 导入文件 file_path = "要导入文件路径" file_name = "要导入文件名" with open(file_path + file_name, "rb") as file: ftp.storbinary("STOR " + file_name, file) # 关闭连接 sock.close() ftp.quit() ``` 请注意,以上示例代码仅演示了如何导入一个文件,并且仅提供了基本的错误处理。根据实际情况,你可能需要添加更多的代码来处理异常、验证文件的存在等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值