I want to loop through hundreds of file names and rename them. The issue is that the file names are over 260 characters and contain characters that make os.rename not usable.
The code below runs with regular filenames but not with the files described earlier:
import os
filepath = "path"
path = os.chdir(filepath)
count = 1
for filename in os.listdir(path):
new_file_name = f"{count}.csv"
os.rename(filename,new_file_name)
count += 1
When I run with the long file names, I get:
Traceback (most recent call last):
File "C:\Users\sarah\Desktop\Python\program.py", line 7, in <module>
os.rename(filename,new_file_name)
FileNotFoundError: [WinError 3] The system cannot find the path specified:
Is there a way of looping through these file names and renaming them?
Filenames cannot exceed 255 characters in Windows of Linux.
Linux:
touch 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
touch: cannot touch ‘zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz’: File name too long
Windows:
c:\Users\cp>echo 'foo' > zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
The filename, directory name, or volume label syntax is incorrect.
[Windows specific]
To overcome the issue with MAX_PATH limitation, replace
os.rename(filename,new_file_name)
with
os.rename("\\\\?\\" + filename, "\\\\?\\" + new_file_name)
why not try linux shell?
c=0
a="a"
t=".txt"
for file in `ls key*`; do
newfile="$a$c$t"
mv $file $newfile
let c=c+1
done
Related
I am trying to read content of a file on my work network from my work network. I copy and pasted a code snippet from a google search and modified it to the below. Why might I still be getting [Errno 2] (I have changed some of the path names for this question board)
The file path in my file explorer shows that "> This PC > word common common" and I don't have "This PC" in my path. I tried adding that into the place I would think it goes in the string. That didn't solve it.
I tried making sure I have matching capitalization. That didn't solve it.
I tried renaming the file to have a *.txt on the end. That didn't solve it.
I tried the different variations of // and / and \ with and without the r predecessor and while that did eliminate the first error I was getting. It didn't help this error.
(Looking at the code errors in the right gutter is says my line length is greater than the PEP8 standard. While I doubt that is the root of my problem, if you can throw in the 'right' wrap method for a file path that long that would be helpful.)
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
Full Error Copy:
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py", line 1, in
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
FileNotFoundError: [Errno 2] No such file or directory: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
EDIT
DEBUG EFFORTS
working to figure out how to change directory. Just in case that is the problem. Tested this code bit
import os
path = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
Received this error
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py", line 5, in <module>
os.chdir(path)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
My intention for adding the picture below is to show how File Explorer displays the file path for my file
FileExplorerPathScreenShot
EDIT
I think this confirms that my 'OS' doesn't have my file.
from os import path
path.exists("PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246")
def main():
print ("File exists:" + str(path.exists('PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246')))
if __name__== "__main__":
main()
Output
File exists: False
I thought OS was a standard variable for Operating system. Now I'm not sure.
EDIT
Using Cmd in DOS, I confirmed that my path for the z: is correct
EDIT - Success
I ran
import os
print( os.listdir("z:/"))
Confirmed I don't need the monster string of folders.
Confirmed, although explorer doesn't show it, it is a *.txt file
Once I implemented these two items the first code worked fine.
Thank you #Furas
To open and read a file specify the filename in your path:
myfile = open("U:/matrix_neo/word common common/hello world.txt", "rt") # open file
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
The U: is a mapped drive in my network.
I did not find any issue with your change dir example. I used a path on my U: path again and it returned True.
import os
path = "U:/matrix_neo/word common common"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
The check the attributes on the directory that you are trying to read from. Also try to copy the file to a local drive for a test and see if you can read the file and also check if it exists.
This is an alternative to the above and uses your path to make sure that the long file path works:
import os
mypath = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
myfile = 'whatever is your filename.txt'
if not os.path.isdir(mypath):
os.makedirs (mypath)
file_path = os.path.join(mypath, myfile)
print(file_path)
if os.path.exists(file_path) is True:
with open(file_path) as filein:
contents = filein.read()
print(contents)
I tested this code using a long csv file.,Replace the variable myfile with whatever is your file name.
I'm getting these errors:
Traceback (most recent call last):
File "file_mover.py", line 41, in <module>
shutil.copy(f, os.path.join(path_to_export_two, Path("/image/")))
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\shutil.py", line 245, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '8ballshake1#8x.png'
When executing this code:
# Making the substrings is more difficult then anticipated prolly just use Java tbh
# LOWERCASE, LOWERCASE THE FOLDERS
import shutil
import os
from pathlib import Path
assets_path = Path("/Users/Jackson Clark/Desktop/uploads")
export_path = Path("/Users/Jackson Clark/Desktop/uploads")
source = os.listdir(assets_path)
"""
NOTE: Filters.js is the important file
The logic:
- Go through each file in the assets_path directory
- Rename the files to start with RoCode (this could be a seperate script)
- Create a new directory with the first four characters of the files name
- Create two sub directories with the names 'image' and 'thumb'
- Copy the file to both the 'image' and 'thumb' directories
That should be all, but who knows tbh
"""
"""
Good links:
https://www.pythonforbeginners.com/os/python-the-shutil-module
https://stackabuse.com/creating-and-deleting-directories-with-python/
"""
for f in source:
f_string = str(f)
folder_one_name = f_string[0:2]
folder_two_name = f_string[2:4]
path_to_export_one = os.path.join(export_path, folder_one_name)
path_to_export_two = os.path.join(export_path, folder_one_name, folder_two_name)
os.mkdir(path_to_export_one)
os.mkdir(path_to_export_two)
os.mkdir(os.path.join(path_to_export_two, Path("/image/")))
os.mkdir(os.path.join(path_to_export_two, Path("/thumb/")))
shutil.copy(f, os.path.join(path_to_export_two, Path("/image/")))
shutil.copy(f, os.path.join(path_to_export_two, Path("/thumb/")))
I simply need the code to generate two folders, the first being named the first two characters of the file that the script is reading, and the second folder (which is a subfolder of the first) to be named the 3rd and 4th characters of the filename that the script is reading. Alas, I'm getting the errors above.
Get rid of the leading slashes in your Paths. These are causing the paths to be truncated:
>>> print(os.path.join("some_folder", Path("/image/")))
\image
>>> print(os.path.join("some_folder", Path("image")))
some_folder\image
The relevant sentence of the os.path.join documentation is as follows:
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
The leading slash in /image/ causes this path component to become absolute and hence the previous components (in my case, just "some_folder") are discarded.
Also, I don't see why you're creating a Path from a string when you can just use a string directly:
>>> print(os.path.join("some_folder", "image"))
some_folder\image
I want to rename photos in folder by it's date. This is my python script.
import os
from datetime import datetime
folder_name = 'D:/Users/user/Desktop/Xiomi/100ANDRO/'
dir_list = [os.path.join(folder_name, x) for x in os.listdir(folder_name)]
for file in dir_list:
filename, file_extension = os.path.splitext(file)
date = datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y_%m_%d_%H_%M_%S')
os.rename(os.path.basename(file), date + file_extension)
print(dir_list)
But I have an error:
$ python script.py
Traceback (most recent call last):
File "script.py", line 10, in <module>
os.rename(os.path.basename(file), date + file_extension)
FileNotFoundError: [WinError 2] ▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒:
'DSC_0003.JPG' -> '2018_07_08_12_28_21.JPG'
The file is definitely in folder. Can you help me with it?
Why are you taking the base name from the target when the absolute path would be okay ?
os.rename works on files that exist. It works if you pass absolute paths provided that the files are located on the same drive. I'd do:
os.rename(file, os.path.join(folder_name,date + file_extension))
basically remove the basename, and add the folder name for the destination. Since the directory is the same for both, that'll work. And it's much cleaner than a dirty os.chdir(folder_name)
Looks to me like you need to give os.rename() the absolute path to the file
os.rename(file, os.path.join(folder_name, date + file_extension))
I'm encountering a troublesome problem with my code and I've been unable to figure it out. Basically I am copying files from a local directory on my computer to a Dropbox folder that acts as a project repository for me and some other folks.
I keep hitting an IO Error when executing the shutil.copy line. Errno 2, N osuch file or directory. However the directory and file both exist. When I change the directory to a different/test location (test_dir in my code), the code runs perfectly fine. Any insights would be greatly appreciated.
import os, os.path
import re
import shutil
import sys
#File Location
directory_list = "path where files are located"
#Dropbox base directory:
dropbox = "path to dropbox directory"
test_dir = "path to test directory on my local machine"
sed_files = os.listdir(directory_list)
for i in sed_files:
#print i.split("BBB")[0]
#df
copy_dir = re.sub(r'XXX',r'_',i.split("BBB")[0])
copy_dir = re.sub(r'ZZZ',r'/',copy_dir)
copy_dir = dropbox + copy_dir + "/FIXED/"
if not os.path.exists(copy_dir):
os.makedirs(copy_dir)
shutil.copy(directory_list+i,copy_dir)
#print directory_list+i
#os.rename(copy_dir+i,copy_dir+i.split("BBB")[1])
Traceback error is:
Traceback (most recent call last):
File "copy_SE_files.py", line 25, in <module> shutil.copy(direcotry_list+i,copydir)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 116, in copy copyfile(src,dst)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 82, in copyfile with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: 'C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed'
Problem solved thanks to the keen eyes of stack overflow. Modified the line to read:
shutil.copy(directory_list+i,'\\\\?\\'+os.path.abspath(copy_dir))
You're failing because the combined length of the path is greater than Window's MAX_PATH limit. C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed is 274 characters long, and without going to some trouble, most Windows file manipulation APIs won't work properly with a path longer than MAX_PATH (which is 260, and one of them is reserved for the NUL terminator).
Assuming Python uses the correct APIs, you can make it work with the extended path prefix, \\?\ (and it may require you to use backslashes rather than forward slashes in your path; I'm not clear on that; read the docs).
The first thing that jumped out at me was this line:
shutil.copy(directory_list+i,copy_dir)
Consider changing it to
shutil.copy(os.path.join(directory_list,i),copy_dir)
IOW, use os.path.join() when concatenating file paths.
One work around is:
try:
shutil.copy(src, dest)
except:
try:
shutil.copy(src, "\\\\?\\" + dest)
#If Long Path as per Maximum Path limitation Windows
except:
self.failed_TC=True
print("Failed to move the script "+os.path.basename(src)+" to "+dest)
I get an IOError shown below when trying to open a new file using "open (fname, 'w+')". The complete error message is below.
The file does not exist, but I verified using "os.access(dir_name, os.W_OK)" and "os.path.exists (dir_name)" that the parent directory for the file does exist.
I am wondering if the file name is just too long for Windows, or if I am doing something wrong. Any tips would be appreciated. Thank you very much.
Error message:
IOError: [Errno 2] No such file or directory: 'C:\Documents and
Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non
Business Hours for
Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link
Utilization\analyzer393146160-data0.js'
In the Windows API the maximum path length is limited to 260 characters.
http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
Update: prepend "\\?\" to the path.
Here is some related code which works for me (I have very long file names and paths):
for d in os.walk(os.getcwd()):
dirname = d[0]
files = d[2]
for f in files:
long_fname = u"\\\\?\\" + os.getcwd() + u"\\" + dirname + u"\\" + f
if op.isdir(long_fname):
continue
fin = open(long_fname, 'rb')
...
Note that for me it only worked with a combination of all of the following:
Prepend '\\?\' at the front.
Use full path, not relative path.
Use only backslashes.
In Python, the filename string must be a unicode string, for example u"abc", not "abc".
Also note, for some reason os.walk(..) returned some of the directories as files, so above I check for that.
You can monkey patch the tarfile module with this:
import tarfile
def monkey_patch_tarfile():
import os
import sys
if sys.platform not in ['cygwin', 'win32']:
return
def long_open(name, *args, **kwargs):
# http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath
if len(name) >= 200:
if not os.path.isabs(name):
name = os.path.join(os.getcwd(), name)
name = "\\\\?\\" + os.path.normpath(name)
return long_open.bltn_open(name, *args, **kwargs)
long_open.bltn_open = tarfile.bltn_open
tarfile.bltn_open = long_open
monkey_patch_tarfile()
If it's not the length of the filename, it's the contents of the filename...
Python is treating '\12' as a control sequence.
>>> fn='C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js'
>>> print fn
C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12bcde_corporate_nov_12.projectbcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks
94897740\json.data\Link\Link Utilizationnalyzer393146160-data0.js
Using raw strings for Windows filenames will help:
>>> fn=r'C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js'
>>> print fn
C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js
Update
Alternatively, use forward slashes '/' instead of backslashes '\', since these will work on all operating systems and will save you hassles with backslashes right at the end of a pathname as in your comments.
See also os.path.join() .
Update 2
Simplified demonstration of problem:
>>> open('.\12\n\r\file.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '.\n\n\r\x0cile.txt'
>>> open('./12/n/r/file.txt')
<open file './12/n/r/file.txt', mode 'r' at 0x7ff83f98>
C:\Users\johnysweb>copy .\12\n\r\file.txt con
Blah
1 file(s) copied.
Check the length of the entire path and then append the necessary Windows long path format. It should be noted that this doesn't work for accessing data from remote directories i.e. paths that begin with '\\some_remote_location\..' so you will need to map that directory locally in order to get "long path" to work.
if len(path_and_file) > 250: #I think the max is 260 but I left a buffer :)
path_and_file = '\\\\?\\'+path_and_file