基本上我的代码运行,但它没有正确执行。我不确定我做错了什么。这是代码。当我在shell中输入内容时,它只是忽略它。我该如何解决?我之前从未真正使用if "___name__ == '_main_':
函数,那么这就是为什么?
import os.path
import os
import shutil
def search_files(direct):
directory=os.path.join(direct)
directory1=os.path.exists(directory)
if directory1:
return directory
else:
print ('ERROR')
direct = input()
def search_characteristics(direct):
interesting = input()
space = interesting.count(' ')
list1 = []
list2 = []
list3 = []
directory = os.path.join(direct)
files = nested_dir(directory)
directory1=os.path.exists(directory)
keyword = interesting[space + 1:len(interesting) + 1]
if interesting[0] == 'N':
for item in files:
if item[item.rfind('\\')+1:item.rfind('.')] == interesting[space+1:len(interesting)+1]:
list3.append(tem)
return list3
elif interesting[0] == 'E' :
for item in files:
if item[item.rfind('.')+1:len(item)+ 1] == ((interesting[space:len(interesting)+1]).strip('')).strip('.'):
list2.append(item)
return list2
elif interesting[0] == 'S' :
for item in files:
if int(keyword) <os.path.getsize(item):
list1.append(item)
return list1
else:
print('ERROR')
def directory_files(nested_directory) -> list:
file_list = []
for element in os.listdirectory(nested_directory):
path = os.path.join(nested_directory,element)
if os.path.isdir(path):
file_list.extend(nested_directory(path))
else:
file_list.append(path)
return file_list
def path(interesting_files):
for files in interesting_files:
print(files)
def read_file(interesting_files):
for files in interesting_files:
infile=file
open_file=open(infile)
for line in open_file.readlines():
print(line)
open_file.close()
def duplicate(interesting_files):
for files in interesting_files:
shutil.copy(files,files+'.dup')
def modify(interesting_files):
for files in interesting_files:
os.utime(files,(1330712280, 1330712292))
if __name__ == '_main_':
while True:
file_input = input("Enter file name")
if file_input.lower() == 'p':
path(operation)
elif file_input.lower() == 'f':
read_file(operation)
elif file_input.lower() == 'd':
duplicate(operation)
elif file_input.lower() == 't':
modify(operation)
else:
print('ERROR')
direct = input()
search_files(direct)
operation = search_characteristics(direct)
print(operation)
operations(operation)
答案 0 :(得分:-2)
如果您要求使用<p>foob<rangy span></span>ar<rangy span></span><i>newtext</i>
和name <- ("Michael Carlos Dumas")
nameSplit <- strsplit(name," ")
,我的获取脚本的方法如下:
[[1]]
[1] "Michael" "Carlos" "Dumas"
我通常将所有功能保留在main
之外,然后使用__name__
作为控制器。如果您尝试通过终端运行它并且它未保存为#!/usr/bin/python
def main():
#your code
if __name__ == "__main__":
main()
,则需要确保main
位于顶部或在脚本名称前键入main
。即如果脚本被调用.py
,您将拥有shebang并执行shebang
或python
。如果您有test_script
个分机号,请尝试./test_script
。希望这有点帮助。还要确保python test_script
脚本的权限。在执行之前,我通常会.py
。