python - When using open() as f: does f represent a new variable being created?
问题描述
I am new(ish) to Python and I'm just trying to figure out how things work. When I use:
with open('C:\\Users\\KB\\Documents\\DataProcessing.txt', 'r') as f:
is f a new variable being created? Could I use any variable in its place? I've seen 'file' used as-well so I'm just wondering if this is an arbitrary name being assigned to a newly created variable? Could it be named anything or does it have to be f?
解决方案
是否正在创建新变量?我可以使用任何变量代替它吗?
是的
with open('testfile.txt') as f:
print(f.read())
相当于
with open('testfile.txt') as lmnopqrstuvwxyz:
print(lmnopqrstuvwxyz.read())
推荐阅读
- psychopy - ValueError:在 TextStim 中设置 pos 时操作不支持的值“0.5”
- react-native - 具有锐利边缘的 React Native 样式视图
- android - Android BLE 扫描在后台某个时间后停止
- php - 除了用户在 Laravel Passport 中注册/登录之外,如何使用不同的模型进行身份验证?
- python - 鹡鸰分页,django,鹡鸰,python
- javascript - 如何有效地将一个对象拆分为数组内的多个对象
- html - 最初在页面加载时出现的单选按钮波纹效果
- linux - 使用 shell 脚本自动输入
- discord.js - 如何从地图中排除具有特定角色的成员?[discord.js]
- python - 循环多个 URL
(来自同一网站的 2 个不同的 URL)