Quote:
Originally Posted by michaelk
Code:
import os
from natsort import natsorted
for file in os.listdir():
print(file)
sorted_files=natsorted(os.listdir())
print(sorted_files)
files from output of ls:
program output:
Code:
1.2.png
screenshot (1573).png
1.3.png
1.21.png
1.10.png
1.1.png
1.11.png
['1.1.png', '1.2.png', '1.3.png', '1.10.png', '1.11.png', '1.21.png', 'screenshot (1573).png']
|
Modified the program, to the one below:
Code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 7 17:49:51 2024
@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
from natsort import natsorted
path = os.chdir(input_dir)
j=1
for file in os.listdir(path):
print(j,':', file)
j+=1
j=1
sorted_files = natsorted(os.listdir(path))
for file in sorted_files:
print('>', j, ':', file)
i=1
for file in sorted_files: #os.listdir(path):
filetype=file.split('.')[-1]
print(input_dir+ '/'+file)
os.rename(input_dir+ '/'+file, input_dir+ '/'+'1.'+str(i)+'.'+filetype)
i +=1
print (file)
But, this gave the error:
Code:
runfile('C:/Users/HP/a.py', wdir='C:/Users/HP')
Traceback (most recent call last):
File D:\Program Files\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)
File c:\users\hp\a.py:9
from natsort import natsorted
ModuleNotFoundError: No module named 'natsort'
So, natsort need be installed on spyder.
Seems there is no way to install natsort on spyder, as got this impression after extensive googling.
On restarting kernel, and applying other means, finally got to the below command:, which also didn't work:
Code:
install natsort
Cell In[5], line 1
install natsort
^
SyntaxError: invalid syntax
Hence, tried to run the above program on jupyter notebook, but that too gives error:
Code:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 2
1 import os
----> 2 from natsort import natsorted
ModuleNotFoundError: No module named 'natsort'
Tried googling for installing natsort, on Anaconda, and got the
results on googling.
But, even these results seem not to help.