I am trying to rename a file in a folder and i keep getting the error that the file is not there....
import os
import time
from os.path import isfile, join
working_dir = ['C:/Users/XXXXX/Desktop']
only_file = [f for f in os.listdir[working_dir] if os.path.isfile[os.path.join[working_dir, f]]]
print only_file
time_srt = time.strftime["%d_%m_%Y"]
if 'EZShift_WeeklyPerDayScheduleReport_Export.xlsx' in only_file:
os.rename['EZShift_WeeklyPerDayScheduleReport_Export.xlsx', "EZShift_" + time_srt + ".xlsx"]
C:\Python27\python.exe C:/Users/xxxxxx/Desktop/Paython/Python3/pbx.py ['xxxxxx.jpg', 'xxxx.zip', 'xxxx.xlsx', 'xxx.pdf', 'xxx.MOV', 'xx.MOV', 'xxxxx_18_12_2016.xlsx', 'EZShift_WeeklyPerDayScheduleReport_Export.xlsx','Test_EZShift_WeeklyPerDayScheduleReport_Export.xlsx'] Traceback [most recent call last]: File "C:/Users/sabaja/Desktop/Paython/Python3/pbx.py", line 24, in os.rename['EZShift_WeeklyPerDayScheduleReport_Export.xlsx', "EZShift_" + time_srt + ".xlsx"] WindowsError: [Error 2] The system cannot find the file specified
Process finished with exit code 1
Try this out, which should help you identify any problems. This will only rename files, and skip subdirectories. import os
import random
import string
directory = "C:\\whatever"
alphabet = string.ascii_lowercase
for item in os.listdir[directory]:
old_fn = os.path.join[directory, item]
new_fn = ''.join[random.sample[alphabet, random.randint[5, 15]]]
new_fn += os.path.splitext[old_fn][1] #adds file extension
if os.path.isfile[old_fn] and not os.path.exists[new_fn]:
os.rename[path, os.path.join[directory, new_fn]]
else :
print 'error renaming {} -> {}'.format[old_fn, new_fn]
If you want to save back to the same directory you will need to add a path to your 'string' variable. Currently it is just creating a filename and os.rename requires a path.
for item in listDir: path = os.path.join[directory, item] for x in random.sample[alphabet, random.randint[5, 15]]: string += x string += path[-4: ] #adds file extension string = os.path.join[directory, string] os.rename[path, string] string = ""
© 2022 Tech Help Notes
Try this out, which should help you identify any problems. This will only rename files, and skip subdirectories.
import os import random import string directory = C: \whatever alphabet = string.ascii_lowercase for item in os.listdir[directory]: old_fn = os.path.join[directory, item] new_fn = .join[random.sample[alphabet, random.randint[5, 15]]] new_fn += os.path.splitext[old_fn][1] #adds file extension if os.path.isfile[old_fn] and not os.path.exists[new_fn]: os.rename[path, os.path.join[directory, new_fn]] else : print error renaming {} - > {}.format[old_fn, new_fn]
If you want to save back to the same directory you will need to add a path to your string variable. Currently it is just creating a filename and os.rename requires a path.
for item in listDir: path = os.path.join[directory, item] for x in random.sample[alphabet, random.randint[5, 15]]: string += x string += path[-4: ] #adds file extension string = os.path.join[directory, string] os.rename[path, string] string =
I have a folder full of pdf files. I'm anycodings_python trying to remove all the spaces from files anycodings_python name and replace them with underscores. anycodings_python Here's what I have so far:,How do I setup python logging to use multiple modules with the output file specified in a function call,just change your directory to the one in anycodings_filenames which the files has to be renamed and anycodings_filenames then follow your code.,I'm guessing there is a pretty simple fix, anycodings_python but I've look all over and cannot find a anycodings_python solution for the life of me.
I have a folder full of pdf files. I'm anycodings_python trying to remove all the spaces from files anycodings_python name and replace them with underscores. anycodings_python Here's what I have so far:
import os, sys folder = path to folder FileList = os.listdir[folder] for files in FileList: if ' ' in files: NewName = files.replace[" ", "_"] os.rename[files, NewName]
When I run this script I get the following anycodings_python error:
WindowsError: [Error 2] The system cannot find the file specified
...
os.rename[os.path.join[folder, files], os.path.join[folder, NewName]]
I found a simple solution for my case. I anycodings_filenames was wanting to rename files and kept anycodings_filenames getting the WindowsError: [Error 2]. anycodings_filenames Simply changing the current directory anycodings_filenames with
and then not trying to work with the anycodings_filenames full path did the trick. Here's the anycodings_filenames relevant lines of script
if [os.path.exists[wd]] == 0: print[wd + " DOES NOT EXIST!!"] sys.exit[] directories = [x[0] for x in os.walk[wd] ] ld = len[directories] dsorted = sorted[directories] print[dsorted] for num in range[1, ld]: currdir = dsorted[num] print["Working on Directory " + currdir] os.chdir[currdir] filenames = next[os.walk[currdir]][2] l = len[filenames] for num in range[0, l]: name = filenames[num] print["Present file " + name] modtime = os.path.getmtime[name]; print[modtime] moddate = datetime.datetime.fromtimestamp[modtime].strftime['%Y %m %d'] moddate = moddate.replace[" ", ""] print[moddate] namesplit = name.split["."] base = namesplit[0] newbase = base + "_" + moddate newname = newbase + "." + namesplit[1] print[newname] os.rename[name, newname] input[]
I found a simple solution for my case. I was wanting to rename files and kept getting the WindowsError: [Error 2]. Simply changing the current directory with , The properties, however remain there, and you can access them through reflection or by using dynamic type of C# 4: , In both cases arrays are strongly typed. In both cases, arrays contain objects of the same anonymous type. The only difference that in the first case the array has the type of the anonymous object, so the compiler knows of properties of the objects inside it, while in the second case the compiler does not know of these properties. , just change your directory to the one in which the files has to be renamed and then follow your code.
I have a folder full of pdf files. I'm trying to remove all the spaces from files name and replace them with underscores. Here's what I have so far:
import os, sys folder = path to folder FileList = os.listdir[folder] for files in FileList: if ' ' in files: NewName = files.replace[" ", "_"] os.rename[files, NewName]
When I run this script I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
...
os.rename[os.path.join[folder, files], os.path.join[folder, NewName]]
I found a simple solution for my case. I was wanting to rename files and kept getting the WindowsError: [Error 2]. Simply changing the current directory with
and then not trying to work with the full path did the trick. Here's the relevant lines of script
if [os.path.exists[wd]] == 0: print[wd + " DOES NOT EXIST!!"] sys.exit[] directories = [x[0] for x in os.walk[wd] ] ld = len[directories] dsorted = sorted[directories] print[dsorted] for num in range[1, ld]: currdir = dsorted[num] print["Working on Directory " + currdir] os.chdir[currdir] filenames = next[os.walk[currdir]][2] l = len[filenames] for num in range[0, l]: name = filenames[num] print["Present file " + name] modtime = os.path.getmtime[name]; print[modtime] moddate = datetime.datetime.fromtimestamp[modtime].strftime['%Y %m %d'] moddate = moddate.replace[" ", ""] print[moddate] namesplit = name.split["."] base = namesplit[0] newbase = base + "_" + moddate newname = newbase + "." + namesplit[1] print[newname] os.rename[name, newname] input[]
Use something like:
The properties, however remain there, and you can access them through reflection or by using
dynamic
type of C# 4:
var x = new Object[] { new {name = "x", phone = 125}, new {name = "f", phone = 859}, new {name="s",phone=584} }; var bigPhone = x.Cast[].Where[v => v.phone > 500]; foreach [dynamic item in bigPhone] { Console.WriteLine["name={0}, phone={1}", item.name, item.phone]; }
You can perform null check.
User user = realm.where[User.class].equalTo["cardId", cardId].findFirst[]; if [user != null] { } else { }