笨办法学Python第十八天:更多文件操作

酒肆小蜗牛 2021-11-25 15:02:07
Python 法学 办法 十八天 笨办法

现在让我们再学习几种文件操作。我们将编写一个 Python 脚本,将一个文件中的内容拷贝到另外一个文件中。这个脚本很短,不过它会让你对于文件操作有更多的了解。

编辑以下内容:

from sys import argv
from os.path import exists

script,from_file,to_file = argv

print "Copying from %s to %s" %(from_file,to_file)

# we could do these two on one line too,how?
input = open(from_file)
indata = input.read()

print "The input file is %d bytes long"%len(indata)

print "Does the output file exist?%r"%exists(to_file)
print "Ready,hit RETURN to continue,CTRL-C to abort."
raw_input()

output = open(to_file,'w')
output.write(indata)

print "Alright,all done."

output.close()
input.close()

你应该很快注意到了我们 import 了又一个很好用的命令 exists。这个命令将文件名字符串作为参数, 如果文件存在的话,它将返回 True,否则将返回 False。在本书的下半部分,我们将使用这个函数做很多 的事情,不过现在你应该学会怎样通过 import 调用它。

通过使用 import ,你可以在自己代码中直接使用其他更厉害的(通常是这样,不过也不尽然)程序员 写的大量免费代码,这样你就不需要重写一遍了。

和你前面写的脚本一样,运行该脚本需要两个参数,一个是待拷贝的文件,一个是要拷贝至的文件。如 果我们使用以前的 test.txt 我们将看到如下的结果:

 

该命令对于任何文件都应该是有效的。试试操作一些别的文件看看结果。不过小心别把你的重要文件给
弄坏了。
版权声明
本文为[酒肆小蜗牛]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_33117229/article/details/121450129

  1. Python爬虫编程思想(85):在Python中使用非关系型数据库
  2. Volume de réglage audio Python (avec Code) | outils Python
  3. Python crawler programming idea (59): get attribute value and text with beautiful soup CSS selector
  4. Python crawler programming idea (57): basic usage of CSS selector in beautiful soup
  5. Python crawler programming idea (55): find of beautiful soup method selector_ All method
  6. Python crawler programming idea (54): use beautiful soup to select sibling nodes
  7. Python crawler programming idea (53): use beautiful soup to select the parent node
  8. Django3.0 solves the problem of error reporting in reverse parsing
  9. Precautions for Python crawler
  10. Python 3 crawler series (1) -- climbing blind date websites
  11. Python到底是什么?为什么要学Python?
  12. #yyds干货盘点#Pandas数据清洗实用指南
  13. Python打包exe文件无法运行
  14. Two common ways to save files in Python
  15. #yyds幹貨盤點#Pandas數據清洗實用指南
  16. Yyds Dry Inventory pandas Data Cleaning Practical Guide
  17. PYTHON用LSTM长短期记忆神经网络的参数优化方法预测时间序列洗发水销售数据
  18. Python集成学习:自己编写构建AdaBoost分类模型可视化决策边界及sklearn包调用比较
  19. Python 3 makes a search software
  20. Python 3 simulated microblog login
  21. Using Python 3 to make practical software for drawing modification
  22. About HTML (acceptable to Python)
  23. Python集成學習:自己編寫構建AdaBoost分類模型可視化决策邊界及sklearn包調用比較
  24. PYTHON用LSTM長短期記憶神經網絡的參數優化方法預測時間序列洗發水銷售數據
  25. Python Integrated Learning: Writing and Constructing adaboost Classification Model Visualized decision Boundary and sklearn package Calling Comparison
  26. Python prédit les données de vente de shampooing de séries chronologiques en utilisant la méthode d'optimisation des paramètres du réseau neuronal de mémoire à court et à long terme lstm
  27. [zero basics of Python to introduction] a prerequisite for Python preparatory knowledge -- basic coding specification of Python
  28. OpenCV对比度亮度变换竟能用来去水印(附Python/C++源码)
  29. [zero basics of Python to getting started] a prerequisite for Python preparatory knowledge -- installing the visualization tool pycharm
  30. The test modifies main.py in micro python
  31. Microphoton experimental circuit board based on mm32f3273 - does not work normally
  32. Run micropathon on mm32f3273 to test performance
  33. Design mm32f3277 micro Python experimental board with SD card
  34. Mm32f3277 corresponding interface files during microphoton migration
  35. Mm32f3277 microphoton experimental board design and software testing
  36. Making and testing mm32f3277 microphoton minimum circuit board
  37. Download mm32-link program automatically with Python simulated mouse
  38. A curriculum of "artificial intelligence Python machine learning and deep learning"
  39. Test the basic functions of mm32 microphoton test circuit board
  40. Test the basic functions of the mm32f3277 micro Python development board flying one by one
  41. Debugging mm32f3277 from zhufei, transplanted with micro Python development board
  42. Recognizing numbers using OpenCV and python
  43. 真的太香了,Python速查表终于出中文版了
  44. Django REST Framework(DRF)教程:快速入门
  45. python django objects filter过滤查询:startswith year
  46. Data encapsulation of Excel test cases in Python
  47. 学员管理系统python
  48. 肝了一夜,8000字概括精髓,pandas必知必会50例!
  49. Python竟然可以画漫画!漫画版的故宫导游图,来袭!
  50. Networkx graph theory Dijkstra algorithm shortest path implementation, Python
  51. Draw a HelloWorld with Python turtle Library
  52. 运行django的服务器内存使用偶尔跳高一点,重启服务马上下来,是内存泄露吧
  53. Basic usage of Python tqdm module in machine learning training
  54. Python -- basic usage method and basic template of argparse module
  55. python-sockertserver并发编程思想
  56. Programmation simultanée du serveur de sockets Python
  57. Vue+Django 旅游网项目 首页前端实现
  58. Classic usage of pandas: iloc and LOC of data filtering
  59. Calendar module of Python time series
  60. A super easy to use Python standard library. It's great to operate directories and files