Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从命令行运行小型多行脚本_Python_Shell - Fatal编程技术网

Python:从命令行运行小型多行脚本

Python:从命令行运行小型多行脚本,python,shell,Python,Shell,我有一些非常小的python脚本,我想从命令行运行它们。这里有一个这样的例子: import os for f in os.listdir(): if not os.path.isdir(f) and f.endswith('.c'): dir=f[:-2] os.makedirs(dir) os.rename( f, dir + '/' + f ) 我非常清楚,我可以将其保存为python脚本(例如,renamer.py),并按如

我有一些非常小的python脚本,我想从命令行运行它们。这里有一个这样的例子:

import os
for f in os.listdir():
    if not os.path.isdir(f) and f.endswith('.c'):
        dir=f[:-2]
        os.makedirs(dir)
        os.rename( f, dir + '/' + f  )  
我非常清楚,我可以将其保存为python脚本(例如,
renamer.py
),并按如下方式运行脚本:

python renamer.py
但是,在编译库时,我有很多这样的小脚本,我只想将它们连接到一个shell脚本中。我就是搞不懂语法。我认为shell脚本应该如下所示:

#!/usr/bin/env bash

python -c/
"import os;"/
"for f in os.listdir():;"/
"    if not os.path.isdir(f) and f.endswith('.c'):;"/
"        dir=f[:-2];"/
"        os.makedirs(dir);"/
"        os.rename( f, dir + '/' + f  );"  
但当我运行此命令时,我得到了错误:

  File "<string>", line 1
    /
    ^
SyntaxError: invalid syntax
./py_test.sh: line 4: import os;/: No such file or directory
./py_test.sh: line 5: for f in os.listdir():;/: No such file or directory
./py_test.sh: line 6:     if not os.path.isdir(f) and f.endswith('.c'):;/: No such file or directory
./py_test.sh: line 7:         dir=f[:-2];/: No such file or directory
./py_test.sh: line 8:         os.makedirs(dir);/: No such file or directory
./py_test.sh: line 9:         os.rename( f, dir + '/' + f  );: No such file or directory
文件“”,第1行
/
^
SyntaxError:无效语法
./py_test.sh:第4行:导入操作系统;/:没有这样的文件或目录
/py_test.sh:第5行:用于os.listdir()中的f;/:没有这样的文件或目录
/py_test.sh:line 6:if not os.path.isdir(f)和f.endswith('.c'):;/:没有这样的文件或目录
/py_test.sh:第7行:dir=f[:-2];/:没有这样的文件或目录
/py_test.sh:第8行:os.makedirs(dir);/:没有这样的文件或目录
/py_test.sh:第9行:os.rename(f,dir+'/'+f);:没有这样的文件或目录

我错过了什么

我想得太多了

这很有效

#!/usr/bin/env bash

python -c "
import os
for f in os.listdir():
    if not os.path.isdir(f) and f.endswith('.c'):
        dir=f[:-2]
        os.makedirs(dir)
        os.rename( f, dir + '/' + f  )  
"

我想得太多了

这很有效

#!/usr/bin/env bash

python -c "
import os
for f in os.listdir():
    if not os.path.isdir(f) and f.endswith('.c'):
        dir=f[:-2]
        os.makedirs(dir)
        os.rename( f, dir + '/' + f  )  
"

最好将它们放在Python模块中,比如说
x.py
作为函数,并使用
Python-c“import x;x.y()”
作为调用它们的命令


然后,您将有一个放置公共代码的位置,您将能够打开文件并获得Python语法高亮显示。

最好将它们放在Python模块中,比如说
x.py
作为函数,并使用
Python-c“import x;x.y()”
作为调用它们的命令


然后,您将有一个放置公共代码的位置,您将能够打开该文件并获得Python语法高亮显示。

我建议您将函数收集到计算机上某个适当的位置(根据Dan d的回答)

但是,我不建议在shell脚本中调用python-c“import-renamer;renamer.rename()”,而是建议在单个python脚本中调用函数,完全避免使用shell脚本:

#!/usr/bin/env python3

import renamer
import other_fun

if __name__ == "__main__":
    renamer.rename()
    ...

我建议您将函数收集到计算机上某个适当的位置(根据Dan D的回答)

但是,我不建议在shell脚本中调用python-c“import-renamer;renamer.rename()”,而是建议在单个python脚本中调用函数,完全避免使用shell脚本:

#!/usr/bin/env python3

import renamer
import other_fun

if __name__ == "__main__":
    renamer.rename()
    ...

很高兴您找到了一个适合您的shell脚本,但我建议您不要这样做。更好的解决方案是在某处保存一堆脚本,并按照您的示例运行
python renamer.py
。然而,更好的解决方案仍然是将函数收集到一个(
def renamer
def other_function
等)中,并在一个Python脚本中调用函数,而不是使用shell脚本。但这并不是否决这个完美解决方案的理由。如果这真的是您所需要的,那么答案很好。很高兴您找到了一个适合您的shell脚本,但我建议您不要这样做。更好的解决方案是在某处保存一堆脚本,并按照您的示例运行
python renamer.py
。然而,更好的解决方案仍然是将函数收集到一个(
def renamer
def other_function
等)中,并在一个Python脚本中调用函数,而不是使用shell脚本。但这并不是否决这个完美解决方案的理由。如果这真的是你所需要的,答案很好。