在DOS下,操作中文目录名和文件名一直是个头疼的问题,如果遇到名字特别长的目录名,则更是困扰,这个程序就是来解决这个问题的。
目前需要改进的地方就是:不知道文件属性的操作。
#
file name : dos.py
# -*- coding: utf-8 -*-
'''
This file is to help removing files and directories with long names.
'''
import os
import sys
# change the directory using typing numbers
def change_directory():
n = 1
dirlist = [] # list for directories
list = os.listdir(os.getcwd())
print " parent directory -- 0 " # this is for the parent directory
# generate directories
for one in list:
if (os.path.isdir(one)):
print one + " -- " + str(n)
dirlist.append(one)
n = n + 1
n = raw_input( " ? " )
# get the directory number that u wanna get in
try :
# if n = 0 , get into the parent directroy
if (int(n) == 0):
try :
os.chdir(os.pardir)
except :
print " there is no parent directory! "
except :
pass
else :
try :
dir = dirlist[int(n) - 1 ]
os.chdir(dir)
except :
pass
# this is for removing directory
def remove_directory():
n = 1
dirlist = [] # list for directories
list = os.listdir(os.getcwd())
# generate directories
for one in list:
if (os.path.isdir(one)):
print one + " -- " + str(n)
dirlist.append(one)
n = n + 1
n = raw_input( " ? " )
# get the number of directory u wanna remove
try :
dir = dirlist[int(n) - 1 ]
os.rmdir(dir)
except :
pass
def del_file():
n = 1
filelist = [] # list for files
list = os.listdir(os.getcwd())
# generate the file list
for one in list:
if (os.path.isfile(one)):
print one + " -- " + str(n)
filelist.append(one)
n = n + 1
n = raw_input( " ? " )
try :
file = filelist[int(n) - 1 ]
os.remove(file)
except :
print " Error! "
def show():
for one in os.listdir(os.getcwd()):
print one
def _help():
print " you can type the following commands "
print " cd change directory "
print " rd remove directory "
print " del delete file "
print " show show files and directories in current directory "
print " help see this help "
if __name__ == " __main__ " :
while (True):
command = raw_input( " > " )
if command == " exit " :
sys.exit( 1 )
elif command == " cd " :
change_directory()
elif command == " rd " :
remove_directory()
elif command == " del " :
del_file()
elif command == " show " :
show()
elif command == " help " :
_help()
else :
continue
# -*- coding: utf-8 -*-
'''
This file is to help removing files and directories with long names.
'''
import os
import sys
# change the directory using typing numbers
def change_directory():
n = 1
dirlist = [] # list for directories
list = os.listdir(os.getcwd())
print " parent directory -- 0 " # this is for the parent directory
# generate directories
for one in list:
if (os.path.isdir(one)):
print one + " -- " + str(n)
dirlist.append(one)
n = n + 1
n = raw_input( " ? " )
# get the directory number that u wanna get in
try :
# if n = 0 , get into the parent directroy
if (int(n) == 0):
try :
os.chdir(os.pardir)
except :
print " there is no parent directory! "
except :
pass
else :
try :
dir = dirlist[int(n) - 1 ]
os.chdir(dir)
except :
pass
# this is for removing directory
def remove_directory():
n = 1
dirlist = [] # list for directories
list = os.listdir(os.getcwd())
# generate directories
for one in list:
if (os.path.isdir(one)):
print one + " -- " + str(n)
dirlist.append(one)
n = n + 1
n = raw_input( " ? " )
# get the number of directory u wanna remove
try :
dir = dirlist[int(n) - 1 ]
os.rmdir(dir)
except :
pass
def del_file():
n = 1
filelist = [] # list for files
list = os.listdir(os.getcwd())
# generate the file list
for one in list:
if (os.path.isfile(one)):
print one + " -- " + str(n)
filelist.append(one)
n = n + 1
n = raw_input( " ? " )
try :
file = filelist[int(n) - 1 ]
os.remove(file)
except :
print " Error! "
def show():
for one in os.listdir(os.getcwd()):
print one
def _help():
print " you can type the following commands "
print " cd change directory "
print " rd remove directory "
print " del delete file "
print " show show files and directories in current directory "
print " help see this help "
if __name__ == " __main__ " :
while (True):
command = raw_input( " > " )
if command == " exit " :
sys.exit( 1 )
elif command == " cd " :
change_directory()
elif command == " rd " :
remove_directory()
elif command == " del " :
del_file()
elif command == " show " :
show()
elif command == " help " :
_help()
else :
continue