【原创】cmd命令行与python_基于powershell的命令行

 

一 起源和官方文档

学命令行,来自于《笨办法学python》这本书,作者强调要先用python2和powershell

查看帮助

help dir

dir /?

 

官方文档

 cmd是新出的powershell的子集

powershell微软官方手册

https://docs.microsoft.com/zh-cn/powershell/scripting/getting-started/fundamental/using-familiar-command-names?view=powershell-6

微软官方:https://docs.microsoft.com/en-us/powershell/

微软官方中文:https://docs.microsoft.com/zh-cn/previous-versions/technet-magazine/hh551144(v=msdn.10)

微软官方office:https://docs.microsoft.com/zh-cn/office/

一些中文参考文档

https://www.cnblogs.com/lavender000/p/6935589.html

https://www.pstips.net/powershell-online-tutorials

http://www.pstips.net/powershell-v3-basic-tutorial.html

http://www.pstips.net/windows-powershell-v3-language-specification.html


 

二 学习命令行的好处

2.1 了解在运行(windows搜索)里敲入

(1) 在运行里可以按 程序名 搜程序和服务

可以在安装软件时方便很多,比如直接在运行里,输入jira,不仅可以查到JIRA的程序,可以调用jira的几个服务,停用jira和启动jira 服务。

PS C:\Users\Administrator> python

PS C:\Users\Administrator> jira

 

(2)还可以学习一些简单的命令,进行本地电脑控制,对远程服务器重启等控制

 运行里输入命令 msconfig 等等

 

 

2.2 敲入cmd 调出cmd窗口后,输入dos命令

(1)cmd命令行:

ipconfig

ipconfig/a 

pwd

dir

 

(2)可以运行对应程序文件

语法:程序关键字  [完整路径/当前目录]文件名

在windows里用可视化的finder只能打开文件编辑,但默认不会执行,但是在命令行里打出文件路径,直至准确的文件名,可以执行,这样就可以试验自己写的脚本是否有错,至少我以前也写过简单的Lua是吧,呵呵

用  lua  xxx.lua

用 python  xxx.txt/ xxx.py

 

 

三 先学习命令行,然后才能学python?

(1)进入和退出python (其他程序同理)

进入:在powershell中打 Python

退出:是用才ctrl+z(敲入后显示为^z)退出python

(2)两种方法

2-1 命令行状态下直接敲命令,注意换行等

2-2 命令行状态下,先写脚本,然后运行脚本

(3)不同运行状态的表现不同

比如在命令行下powershell下,Python下直接运行语句,每次都会有返回结果

继续在cmd的普通目录下,运行 python执行脚本,根据脚本内容返回结果

 

 

四 基本快捷键

(1)命令行里,按上箭头可重复上次的打字内容,还可以多按几次箭头,可以省事点方便

有的是ctrl+n

(2)复制不是crtl+C,而是鼠标左键按住选中要复制的文本,点一下右键即可,然后再需要粘贴的地方也点下鼠标右键即可。

(3)Ctrl+Z 退出Python

         CTRL+C 打断

 

 

五 详细的用powershell学习命令行的记录

命令行列表(我自己验证过了---现在都没加开关参数 /a 等等)

powershelldos/cmd英文名说明
pwdchdirprint working directory打印出当前的工作目录
hostnamehostname 你使用的电脑在网络中的名字
md / mkdirmd / mkdirmake dir创建路径(文件夹)
cdcdchange directory更改路径
new-item  创建文档,需要很多参数
dir/lsdirdos--linux下才可用

列出路径下的所有内容

可以带参数的

rmdir/rmrmdirremove删除路径,
pushd 

联动,如输入

md folder888

pushd folder888

popd

推送路径
popd  

弹出路径

返回之前的路径,逐层返回

cp/copycopycopy复制路径/文件
robocopy  更可控的复制命令
mv move移动路径/文件
more  逐页显示整个文件内容
type  打印输出整个文件内容
forfiles  在一堆文件上运行一条命令
dir  dir -r寻找文件
select-string  在文件内查找内容
help  阅读帮助手册
helpctr  寻找合适的手册页面
echo  打印一些参数
set  设定/导出一个新的环境变量
exit  退出powershell
runas  成为超级用户或root,危险命令

 

(1)第一个学习pwd,本来的全称:print working directory,显示当前路径

     pwd (打之前记得先退出python)

     cd ~ (回到根目录,home路径,记得是 cd ~ 中间有空格)

 

(2)在根目录下,可运行程序

比如PS C:\Users\Administrator> python 或PS C:\Users\Administrator> jira,进入到这些功能后,就不在根目录了,所以按现在教程的逐级文件夹往下查就不行(高手估计是可以的),所以用先pwd 然后cd ~出来是不行的,得用ctrl+z退出程序,然后就回到了,文件模式的根目录了。

PS C:\Users\Administrator> python

^z (需要输入crtl+z)

 

(3)windows的特别之处:/等于\,也就是 C:\Users\Administrator  等同于 C:/Users/Administrator 

        其他地方一般都只用/ (斜杠),比如各种其他操作系统,网址等等,windows这点有点奇葩?

       所以代码使用时一般使用  C:\\Users\\Administrator   或者C:/Users/Administrator 

       因为很多程序里 \ 会被认为是转义!

       输入文件路径,要么是 \\ 或 / 路径,不要输入\ 作为路径分隔符

 

(4)mkdir   (新建路径 make dir)(只能创建目录)

      都是在根目录下创建,因为你还没有切换目录啊(用cd命令),而且创建新目录后,并不切换目录,还留在原处。

poewrshell

mkdir temp

mkdir temp\test1                powershell两种/ 和 \ 都可以 temp/test1

mkdir temp\test\test11

mkdir "I have fun"    //路径里如果包含空格,得用引号

mkdir -p  temp/test/test11 //即使中间目录不存在也能成功?创建完整的?

 

dos 

mkdir temp\test1                后面这是错误语法  temp/test1

 

 

(4-2)创建文件

powershell 创建,需要用 new-item

 

 

cmd 下创建文档--很多方法

https://www.cnblogs.com/kedarui/p/3987656.html

echo 111>1.txt

cd.>2.txt

copy nul 3.txt

 

 

(5)cd  切换路径

cd temp

cd ~           // 写成cd~会报错,回到根目录

cd ..           //往上回一级

cd ../..        //往上回2级

cd ../../..    //往上回3级

cd "I have fun"   // 只能 "I have fun"   上一层输入,也就是必须逐级输入,没法直接查找某个文件夹


(6)ls 显示当前路径下的内容:子一级的文件夹/文件(类dir功能吧)

如果文件夹内是空的,输出就啥也不显示了

其实一般是这样连用的   cd joe  ----ls---找到下级要去的--cd alex ----ls....

ls            //显示本级的

ls test1/    // 显示一个本级的子目录下的内容

 

 

(7) 输入 help,还要继续选下级,比如get-command。。好多内容

 

(8)rmdir 删除空文件或空目录 (remove dir) (可以删除目录,也可以删除文件)

rmdir test1   //如果test1里有内容,会提示是否全部删除  Y   A     N之类,选择yes可以连下级都删除,全部删除
                   //也就说是,只有目录里是空的,才不提示直接删,否则会询问用户,做抉择。

 

(9)pushd 切换路径   push dir

保存当前路径,切换到新路径

 

(10) popd 回到上一个路径   pop dir

回到上一个路径

 

(11) touch  --苹果的,等同new-item

(12) new-item   创建空文件/目录

        new-item  -type file  创建文件 ,如果不带 -type 下一步会被询问,输入file亦可(或者dir)

        new-item  debug.txt -type file

       new-item  debug.xls  -type file

       new-item   storage -type dir

 

(13) cp 复制文件至一个新文件名/或某路径下,名字不变

   只能往同级或下层去操作,不能操作上层?

  可以把目录/文件 往另外一个目录里放,但好像只有1层目录,下层的不会?

     cp  test1.txt  test2.txt

    cp   test1.txt     test2222

     cp  test1.txt  alreadydir/

    cp   testdir1  testdir2/

    cp   testdir1/test1.txt   testdir2/

      以前不会 从同级的desktop  复制1个文件到 temp 下去--------其实就是用相对或绝对路径复制过去即可

             cp Desktop\\desktop1.txt temp\\123

首先,cmd和powershell也支持中文路径

  cd 桌面     

  cd Desktop 

 

 

(14) mv    移动文件/文件夹目录  move,本质就是重命名了rename, 原名的文件消失了

如果新文件名没有则自动创建,如果有就直接移动过去,老的消失

   mv test1  test111

   mv  test1.txt  test111.txt 

  参数

  mv  -a 

  mv  -r  带下层目录递归

  mv -f

 

(15)less  苹果用

(16)more 显示文件内容

more test.txt

 

(17)cat 流文件内容显示,和more的区别是?--现在我觉得用起来是一样的,以后慢慢发现和学习吧

cat test.txt

 

(18)rm 删除文件

rmdir不是也可以删除文件吗?区别是?

(19)exit

(20) forfiles

(21)runas

(22)attrib

(23)icacls

 

 

看了下官方手册,好多内容,好强大

比如还有好多有用的命令,在windows操作系统的机器上,估计编程很多时候还需要用到这些命令。。。。

比如cls  (clear screen) 

shutdown.exe -1

stop-computer

restart-computer

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CHAPTER 1 About Python ............................................................................................1 What Is Python? ................................................................................................................1 A Brief History of Python ................................................................................................2 Interpreters Versus Compilers .......................................................................................5 When to Use (or Not Use) an Interpreted Language .........................................8 Understanding Bytecodes ......................................................................................10 Why Use Python? ...........................................................................................................11 Object-Oriented ........................................................................................................11 Cross Platform ..........................................................................................................11 Broad User Base .......................................................................................................11 Well Supported in Third-Party Tools ...................................................................12 Good Selection of Tools Available ........................................................................12 Good Selection of Pre-built Libraries ..................................................................12 Where Is Python Used? .................................................................................................13 How Is Python Licensed? ..............................................................................................13 Where Do I Get Python? ...............................................................................................14 Installing Python ............................................................................................................14 Getting Information on Python ..................................................................................16 Python Communities .....................................................................................................17 Other Software ................................................................................................................18 And Now for Something Completely Different… ....................................................18 CHAPTER 2 Python Language Overview .................................................................19 Python Syntax .................................................................................................................20 Comments .................................................................................................................20 Indentation ...............................................................................................................20 Contents TABLE OF} vii Q Q Q Python Reserved Words ................................................................................................24 Decision Making and Iteration Keywords ..........................................................25 Debugging Keywords ..............................................................................................27 Package and Module Handling Keywords .........................................................27 Exception Handling Keywords ..............................................................................29 General Language Keywords .................................................................................31 Other Keywords ........................................................................................................32 Variable Usage ................................................................................................................34 The Continuation Variable ....................................................................................36 Watching Out for Spelling Mistakes! ..................................................................37 Predicates .........................................................................................................................38 Identifier Scope ...............................................................................................................39 Operators .........................................................................................................................42 Modulo Operator .....................................................................................................44 Exponential Operator .............................................................................................46 Logical Operators ....................................................................................................46 Comparative Operators ..........................................................................................49 Bitwise Operators ....................................................................................................51 Membership Operators and String Operators ..................................................53 Identity Operators ...................................................................................................53 In Conclusion ..................................................................................................................53 CHAPTER 3 Tools ..........................................................................................................55 IDLE ...................................................................................................................................55 File Menu ...................................................................................................................57 The Path Browser Dialog ........................................................................................62 Edit Menu ..................................................................................................................64 Shell Menu .................................................................................................................70 Debug Menu ..............................................................................................................71 The Edit Window ......................................................................................................79 Format Menu ............................................................................................................80 CONTENTS viii Q Q Q Command Line Compiler ..............................................................................................90 Creating Python Files ....................................................................................................93 Documentation ...............................................................................................................95 In Conclusion ..................................................................................................................96 CHAPTER 4 Data Types ...............................................................................................97 Numeric Types ................................................................................................................98 Integers ......................................................................................................................98 Demonstrating Long Integers ...............................................................................99 Octal and Hexadecimal ........................................................................................100 Floating Point Numbers .............................................................................................101 Strings ............................................................................................................................103 String Variables .....................................................................................................103 Concatenating Strings .........................................................................................106 Repeating Strings ..................................................................................................107 Substrings ...............................................................................................................108 Slicing ......................................................................................................................110 String Functions ....................................................................................................111 String Constants ....................................................................................................112 Conversion Functions ...........................................................................................114 Search Functions ...................................................................................................118 Formatting Functions ..........................................................................................120 Escape Sequences ..................................................................................................121 Sequences ......................................................................................................................122 Lists ..........................................................................................................................123 Shared References .................................................................................................128 Tuples .......................................................................................................................128 Dictionaries ............................................................................................................132 Advanced Type .............................................................................................................136 Classes and Objects ..............................................................................................136 Complex Type .........................................................................................................137 Generator Type ......................................................................................................138 CONTENTS ix Q Q Q None Type ...............................................................................................................139 Unicode Type ..........................................................................................................140 In Conclusion ................................................................................................................141 CHAPTER 5 Control Flow .........................................................................................143 Conditionals ..................................................................................................................144 The if Statement ..................................................................................................144 The elif Statement .............................................................................................147 The else Statement .............................................................................................149 Wrapping Up the Conditionals: A Cool Example ...........................................150 Loops ..............................................................................................................................153 The for Loop ..........................................................................................................153 The while Loop .....................................................................................................161 In Conclusion ................................................................................................................164 CHAPTER 6 Input and Output ................................................................................165 User Input ......................................................................................................................165 The input Function ..............................................................................................166 The raw_input Function ....................................................................................168 User Output ...................................................................................................................170 Formatting ..............................................................................................................172 File Input .......................................................................................................................175 File Output ....................................................................................................................177 Closing Files ..................................................................................................................179 Positioning in Files ......................................................................................................180 Directories and Files ...................................................................................................183 The stat Module: File Statistics ..............................................................................186 Command Line Arguments ........................................................................................190 Pickle ..............................................................................................................................192 In Conclusion ................................................................................................................195 CONTENTS x Q Q Q CHAPTER 7 Functions and Modules .....................................................................197 What Is a Function? ....................................................................................................197 Defining Functions in Python ...................................................................................197 What Are Arguments? ................................................................................................200 How Do You Pass an Argument to a Function? ....................................................201 Default Arguments ......................................................................................................203 Variable Default Arguments .....................................................................................205 Keyword Arguments ....................................................................................................206 Returning Values from Functions ............................................................................207 Returning Multiple Values from Functions ...........................................................209 Recursive Functions ....................................................................................................210 Passing Functions as Arguments .............................................................................212 Lambda Functions .......................................................................................................213 Variable Numbers of Arguments to a Function ....................................................215 Variable Scope in Functions ......................................................................................216 Using Modules ..............................................................................................................218 In Conclusion ................................................................................................................219 CHAPTER 8 Exception Handling ............................................................................221 Looking at Exceptions in Python ..............................................................................222 Traceback Example ......................................................................................................223 Understanding Tracebacks ........................................................................................224 Exceptions .....................................................................................................................225 Catching Exceptions with try..except .........................................................226 Multiple except Clauses .....................................................................................229 Blank except Clauses ..........................................................................................231 The else Clauses .........................................................................................................232 The finally Clause ....................................................................................................234 Raising Your Own Exceptions ...................................................................................235 Exception Arguments .................................................................................................237 User-Defined Exceptions ............................................................................................238 Working with the Exception Information ..............................................................239 CONTENTS xi Q Q Q exc_type ................................................................................................................239 exc_value ..............................................................................................................240 Using the with Clause for Files ................................................................................243 Re-throwing Exceptions .............................................................................................244 In Conclusion ................................................................................................................246 CHAPTER 9 Object-Oriented Programming .........................................................247 A Brief History of OOP ................................................................................................247 What Is an Object? ......................................................................................................248 Why Do We Use Objects? ...........................................................................................249 Reuse ........................................................................................................................249 Ease in Debugging .................................................................................................250 Maintainability ......................................................................................................250 The Attributes of Object-Oriented Development .................................................251 Abstraction .............................................................................................................251 Data Hiding ............................................................................................................252 Inheritance .............................................................................................................253 Polymorphism ........................................................................................................255 Terminology ..................................................................................................................256 Class .........................................................................................................................256 Object .......................................................................................................................256 Attribute ..................................................................................................................257 Method ....................................................................................................................258 Message Passing ....................................................................................................259 Event Handling ......................................................................................................260 Derivation ...............................................................................................................260 Coupling ..................................................................................................................261 Cohesion ..................................................................................................................261 Constants ................................................................................................................261 Other Concepts .............................................................................................................262 In Conclusion ................................................................................................................262 CONTENTS xii Q Q Q CHAPTER 10 Classes and Objects in Python ..........................................................265 Python Classes ..............................................................................................................265 Properties ......................................................................................................................267 Attribute Modifying Functions .................................................................................272 Private Attributes ........................................................................................................274 Doc Strings ....................................................................................................................275 Properties ......................................................................................................................277 The self Object ...........................................................................................................279 Methods .........................................................................................................................281 Special Methods ...........................................................................................................283 Initialization ...........................................................................................................283 Termination ............................................................................................................284 String Conversion ..................................................................................................285 Inheritance ....................................................................................................................287 Multiple Inheritance ...................................................................................................291 Using super ..................................................................................................................293 Polymorphism ..............................................................................................................295 Exception Classes .........................................................................................................297 Iterators .........................................................................................................................299 Operator Overloading .................................................................................................301 In Conclusion ................................................................................................................304 CHAPTER 11 The Python Library ..............................................................................305 Containers .....................................................................................................................305 Working with the deque Class ...........................................................................306 Math ................................................................................................................................312 Complex Math ..............................................................................................................313 Types ...............................................................................................................................315 Strings ............................................................................................................................318 Regular Expressions ....................................................................................................319 Patterns ...................................................................................................................320 Special Sequence Characters ..............................................................................323 CONTENTS xiii Q Q Q Compiling Regular Expressions .........................................................................323 Matching Strings ...................................................................................................324 Meta Characters ....................................................................................................326 Grouping .................................................................................................................327 System ............................................................................................................................328 Random Number Generation ....................................................................................330 Dates and Times ...........................................................................................................331 Creating a New Time ............................................................................................332 Time Operations ....................................................................................................332 Creating a New Date .............................................................................................333 Date Operations ....................................................................................................333 Time Zone Information ........................................................................................335 Operating System Interface .......................................................................................336 System Information ..............................................................................................336 Process Management ...........................................................................................337 In Conclusion ................................................................................................................341 CHAPTER 12 The GUI — TkInter ...............................................................................343 What Is TkInter? ...........................................................................................................343 Terms and Conditions .................................................................................................343 Event Handling ......................................................................................................344 Callbacks .................................................................................................................344 Widgets ....................................................................................................................345 Layout Managers ...................................................................................................345 Working with TkInter .................................................................................................346 Creating a Label ...........................................................................................................347 Frame Widgets and Centering ..................................................................................349 An Application with a Button ...................................................................................351 Working with Entry Fields and Grid Layouts ........................................................353 Creating a Class to Handle User Interfaces ...........................................................356 Working with List Boxes ............................................................................................358 Scrolling a List Box ................................................................................................361 CONTENTS xiv Q Q Q Menus .............................................................................................................................363 Context Menus .............................................................................................................366 Scale Widgets ................................................................................................................367 RadioButtons and CheckButton ...............................................................................370 Text Widgets .................................................................................................................373 In Conclusion ................................................................................................................375 CHAPTER 13 The Web Server—Apache ...................................................................377 Setting Up Apache .......................................................................................................377 Testing Apache .............................................................................................................378 Your First Python CGI Script: Hello Apache ...........................................................379 Examining the Hello Python Script ...................................................................380 The cgi-bin Directory ............................................................................................381 A Script for Displaying the Environment ...............................................................382 Receiving Data from an HTML File ..........................................................................384 Sending Data to an HTML File ..................................................................................387 How It All Works ...................................................................................................390 Dynamic HTML Displays Based on User Input ......................................................391 HTML Elements ............................................................................................................396 Cookies ...........................................................................................................................399 Uploading Files ............................................................................................................402 Redirection ....................................................................................................................403 Error Handling ..............................................................................................................405 In Conclusion ................................................................................................................406 CHAPTER 14 Working with Databases ...................................................................407 What Is a Database? ...................................................................................................407 Simple Database Terminology ...........................................................................408 What Is MySQL? ...........................................................................................................409 Downloading and Installing ......................................................................................409 Creating a New Database ..........................................................................................410 Creating a New User ....................................................................................................414 CONTENTS xv Q Q Q Opening an Existing Database .................................................................................415 Writing to a Database ................................................................................................417 Reading from a Database ..........................................................................................421 Updating a Database ..................................................................................................424 Deleting from a Database ..........................................................................................427 Searching a Database .................................................................................................430 In Conclusion ................................................................................................................436 CHAPTER 15 Putting It All Together .......................................................................437 Designing the Application .........................................................................................437 Program Flow .........................................................................................................437 User Interface Design ...........................................................................................438 Database Design ...................................................................................................439 Implementing the Database Tables ........................................................................440 Implementing the Forms ...........................................................................................442 Adding Reviews ............................................................................................................449 Adding the Review to the Database ........................................................................452 Listing the Reviews ......................................................................................................456 Deleting Books .............................................................................................................459 In Conclusion ................................................................................................................462 CHAPTER 16 Python and Graphics ..........................................................................463 The PIL Library ..............................................................................................................463 Downloading ..........................................................................................................464 Installing .................................................................................................................464 Verifying Your Installation ..................................................................................465 Creating a New Image ................................................................................................465 Function Parameters ............................................................................................466 Drawing on the Image ................................................................................................467 Drawing the Image ...............................................................................................468 Displaying the Image ...........................................................................................470 CONTENTS xvi Q Q Q

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值