Я столкнулся с следующей ошибкой при попытке переименовать файл на телефоне Android, используя ADB в Windows. Команда MV часто дает мне следующую ошибку:
mv: '/storage/emulated/0/Download/xrn/folder/new_name.xml' not directory
< /code>
Команда командной строки ADB: < /p>
adb shell mv /path/to/old /path/to/new
< /code>
Мне нужно было автоматически переименовать множество файлов, и у меня были причины не делать это непосредственно на устройстве с помощью Python или приложения, поэтому я использовал функцию Python, которая перечисляет каталоги и переименовал все файлы в них в соответствии с функцией, которую я написал: < /p>
import subprocess
def list_files_and_dirs(path):
# Run the adb command to list files and directories
result = subprocess.run(['adb', 'shell', 'ls', '-l', path],
capture_output=True, text=True)
# Initialize the result dictionary
ret = {'files': [], 'dirs': []}
# Check if the command was successful
if result.returncode == 0:
# Split the output into lines
lines = result.stdout.splitlines()
for line in lines:
# Skip empty lines
if not line.rstrip():
continue
print(line)
# Split the line into parts (you may need to alter this line according to your adb shell ls output format)
try :
name = line.split(':')[1][3:].replace('\\', '')
except :
name = ''
if name != '' :
# The first part indicates the type (d for directory, - for file)
entry_type = line[0]
# The last part is the name of the file or directory
if entry_type == 'd':
ret['dirs'].append(name)
elif entry_type == '-':
ret['files'].append(name)
else:
print(f"Error: {result.stderr.strip()}")
return ret
def new_name(n) :
return b.b64encode(n. encode('utf-8')). decode('cp1250'). replace('+', '-'). replace('/', '_'). replace('=', '') + '.bn'
def rename_file(old_name, new_name):
# Construct the adb command to rename the file
command = ['adb', 'shell', 'mv', old_name, new_name]
# Run the command
result = subprocess.run(command, capture_output=True, text=True)
print(command)
# Check if the command was successful
if result.returncode == 0:
print(f"Successfully renamed '{old_name}' to '{new_name}'")
else:
print(f"Error: {result.stderr.strip()}")
def walk (p, l = 0) :
array = list_files_and_dirs(p)# + '/*')
for i in array['dirs'] :
walk(p + '/' + i, l + 1)
for i in array['files'] :
print(p, '/', array)
name = new_name(i)
if '.bn' not in i :
#print(i, name)
rename_file('%s/%s' % (p, i), '%s/%s' % (p, name))
root = '/storage/emulated/0/Download/xrn'
walk(root)
< /code>
Функция иногда не удается с сообщением об ошибке < /p>
mv: '/storage/emulated/0/Download/xrn/folder/new_name.xml' not directory
Я столкнулся с следующей ошибкой при попытке переименовать файл на телефоне Android, используя ADB в Windows. Команда MV часто дает мне следующую ошибку: [code]mv: '/storage/emulated/0/Download/xrn/folder/new_name.xml' not directory < /code> Команда командной строки ADB: < /p> adb shell mv /path/to/old /path/to/new < /code> Мне нужно было автоматически переименовать множество файлов, и у меня были причины не делать это непосредственно на устройстве с помощью Python или приложения, поэтому я использовал функцию Python, которая перечисляет каталоги и переименовал все файлы в них в соответствии с функцией, которую я написал: < /p> import subprocess
def list_files_and_dirs(path): # Run the adb command to list files and directories result = subprocess.run(['adb', 'shell', 'ls', '-l', path], capture_output=True, text=True)
# Initialize the result dictionary ret = {'files': [], 'dirs': []}
# Check if the command was successful if result.returncode == 0: # Split the output into lines lines = result.stdout.splitlines()
for line in lines: # Skip empty lines if not line.rstrip(): continue print(line) # Split the line into parts (you may need to alter this line according to your adb shell ls output format) try : name = line.split(':')[1][3:].replace('\\', '') except : name = ''
if name != '' : # The first part indicates the type (d for directory, - for file) entry_type = line[0] # The last part is the name of the file or directory
if entry_type == 'd': ret['dirs'].append(name) elif entry_type == '-': ret['files'].append(name)
def rename_file(old_name, new_name): # Construct the adb command to rename the file command = ['adb', 'shell', 'mv', old_name, new_name]
# Run the command result = subprocess.run(command, capture_output=True, text=True)
print(command)
# Check if the command was successful if result.returncode == 0: print(f"Successfully renamed '{old_name}' to '{new_name}'") else: print(f"Error: {result.stderr.strip()}")
def walk (p, l = 0) : array = list_files_and_dirs(p)# + '/*') for i in array['dirs'] : walk(p + '/' + i, l + 1) for i in array['files'] : print(p, '/', array) name = new_name(i) if '.bn' not in i : #print(i, name) rename_file('%s/%s' % (p, i), '%s/%s' % (p, name))
root = '/storage/emulated/0/Download/xrn' walk(root) < /code> Функция иногда не удается с сообщением об ошибке < /p> mv: '/storage/emulated/0/Download/xrn/folder/new_name.xml' not directory [/code] Есть идеи?