适合初学者的python实际例子
乙醇 创建于 over 6 years 之前
最后更新: less than a minute 之前
阅读数: 492
最近在github上发现了一个有意思的项目,很适合初学者学习python代码。
学习一门语言刚开始的时候是很枯燥的,各种概念语法以及无聊的打印都会让人失去更进一步学习的动力。
很多同学在学习了一段时间之后甚至会怀疑学习语言的用处,因为总是写不出东西,只会写一些简单的打印代码。
这个叫做geekcomputers/Python的项目很好的回答了一个问题,就是学习python可以做什么。
仔细观察里面的例子,我们可以发现写一些简单的python代码就可以做很多有意思的事情,比如
- 批量修改文件的后缀名
- 显示文件的一些信息
- 测试某个文件夹是否存在,不存在就自动创建
- 搜索所有目录下的log文件,并自动压缩归档
- 把所有创建时间超过240天的文件移动到某个文件夹
- 自动发twitter
- 下载最新的漫画
- 下载视频网站的视频
- ......
这些例子的代码都很简单,推荐大家这样学习
- 首先照着这些代码敲一遍
- 让代码可以正确运行
- 为每一行代码写上注释
- 只保留注释,去掉具体代码,看看自己能不能写出来
相信经过一段时间的努力,你应该可以熟练使用python去实现一些简单有趣的功能了。
最后附上例子的链接。
batch_file_rename.py - This batch renames a group of files in a given directory, once you pass the current and the new extensions.
create_dir_if_not_there.py - Checks to see if a directory exists in the users home directory, if not then create it.
Fast Youtube Downloader - Downloads youtube videos quickly with parallel threads using aria2c
Google Image Downloader - Query the specific term and retrieve images from the google image database.
dir_test.py - Tests to see if the directory
testdir
exists, if not it will create the directory for you.env_check.py - This script will check to see if all of the environment variables required are set.
fileinfo.py - Shows file information for a given file.
folder_size.py - Scans the current directory and all subdirectories and displays the size.
logs.py - This script will search for all
*.log
files in the given directory, zip them using the program you specify, and then date stamp them.move_files_over_x_days.py - Moves all files over 240 days old from the source directory to the destination directory.
nslookup_check.py - This simple script opens the file
server_list.txt
and then does an nslookup for each one to check the DNS entry.osinfo.py - Displays some information about the OS on which you are running this script.
ping_servers.py - This script, depending on the arguments supplied, will ping the servers associated with that application group.
ping_subnet.py - After supplying the first 3 octets this file scans the final range for available addresses.
powerdown_startup.py - This file goes through the server list and pings the machine, if it is up it will load the putty session, if it is not it will notify you.
puttylogs.py - This file zips up all the logs in the given directory.
script_count.py - This file scans the scripts directory and gives a count of the different types of scripts.
script_listing.py - This file will list all the files in the given directory, and go through all the subdirectories as well.
testlines.py - This simple script opens a file and prints out 100 lines of whatever is the set for the line variable.
tweeter.py - This script allows you to tweet text or a picture from the terminal.
serial_scanner.py contains a method called ListAvailablePorts which returns a list with the names of the serial ports that are in use in the computer, this method works only on Linux and Windows (can be extended for mac osx). If no port is found, an empty list is returned.
get_youtube_view.py - This is very simple python script to get more views for your youtube videos. Useful for repeating songs on youtube.
CountMillionCharacter.py And CountMillionCharacter2.0.py - Counts character scripts, or how much characters are present on any text based file.
xkcd_downloader.py - Downloads the latest XKCD comic and places them in a new folder called "comics".
timymodule.py - A great alternative to Pythons 'timeit' module and easier to use.
calculator.py - Uses Python's eval() function to implement a calculator.
Google_News.py - Uses BeautifulSoup to provide Latest News Headline along with news link.
cricket_live_score - Uses BeautifulSoup to provide live cricket score.
youtube.py - Takes a song name as input and fetches the youtube url of the best matching song and plays it.