Im ney to python and therefore apologize for the triviality of my question:
I have the following file structure, where a .csv file with employee is saved on daily basis:
dir/2012-01-01/employee.csv.bz2
dir/2012-01-02/employee.csv.bz2
dir/2012-01-03/employee.csv.bz2
dir/2012-01-04/employee.csv.bz2
dir/2012-01-05/employee.csv.bz2
I would like to go through each file and rename it. Afterwards I would like to save the new files in one common directory dir/common. What I tried:
import sys
import os
path = 'dir/'
for folderName, subfolders, filenames in os.walk(path):
for filename in filenames:
infilename = os.path.join(path, filename)
newname = infilename.replace('.csv.bz2', '.csv')
output = os.rename(infilename, newname)
But I get the error:
output = os.rename(infilename, newname)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'dir/employee.csv.bz2' -> 'dir/employee.csv'
Not sure what Im doing wrong.
Use folderName instead of path in os.path.join(path, filename) because folderName has full path to subfolder.
infilename = os.path.join(folderName, filename)
If you want to save in one folder then use this folder in newname and rename() will move file to new place.
newname = os.path.join('dir', 'common', filename.replace('.csv.bz2', '.csv'))
BTW: But you have to create this folder first.
os.mkdir('dir/common')
or to create folder and all intermediate folders from path
os.makedirs('dir/common/many/sub/folders/to/create')
infilename = os.path.join(path, filename)
You are missing the subfolder, which is evident from the error message: 'dir/employee.csv.bz2'.
Related
I am trying to make a program that can capitalize any file name.
here is my code:
import os
file = os.walk("E:\Work\experiment\ex")
for root, dirs, files in file:
fil = files[1]
cap = fil.capitalize()
new_name = os.renames(fil, cap)
print(new_name)
But I am getting this error:
[WinError 2] The system cannot find the file specified: 'basic_problm_2.py' -> 'Basic_problm_2.py'
You are forgetting that fil is equal to the name of file. When you call os.renames, it looks for that file in the current directory and it doesn't find it, thus the error is raised. You have to send absolute path to the renames method:
import os
file = os.walk("E:\Work\experiment\ex")
for root, dirs, files in file:
fil = files[1]
cap = fil.capitalize()
new_name = os.renames(os.path.join(root, fil), os.path.join(root, cap))
print(new_name)
os.path.join simply joins the arguments it takes. root is the directory where the fil is, so os.path.join(root, fil) will give you the absolute path.
import os
path = "C:/foo/foo/test/"
for filename in os.listdir(path):
if os.path.isfile(os.path.join(path, filename)):
os.rename(
os.path.join(path, filename), os.path.join(path, filename.capitalize())
)
This should do the trick. I'll also recommend using forward slash / with directories as backslash \ can cause issues (Another option is you can use // instead).
I have zip files in folder A and I want to unzip it and rename the pictures in the unzip file based on the name of the zip files then move it to folder B. But I don't know how to rename it. Please help!
I have script like this:
path = r'C:\Users\A'
path_to_extract = r'C:\Users\B'
for root, dirs, files in os.walk(path):
for file in files:
if '.zip' in str(file):
file_path = path + '/' + file
zip_ref = zipfile.ZipFile(file_path, 'r')
zip_ref.extractall(path_to_extract)
You could do it like this, don't forget to put the extension behind the filename
import os
os.rename(r'file path with old filename',r'file path with new filename')
In my code in Python I keep getting the error
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Z_Cawc_000_005_estimated_value.tfw' -> 'awc_000_005_estimated_value.tfw'
So,
'Z_Cawc_000_005_estimated_value.tfw' is the name of the first raster on a folder with 800 rasters.
All I want is to get rid off the initial "Z_C" characters of the 800 raster files in a folder.
My code is
import os
path = os.getcwd()
filenames =
os.listdir(r'I:\PhD_2019\Spatial_Datasets\Baroon_Pocket_Dam_Catchment\Raster\Soil_Zonal_Stats')
for filename in filenames:
os.rename(filename, filename.replace("Z_C",""))
I`ve also try the following code:
from os import rename, listdir
badprefix = "Z_C"
fnames = listdir('I:\\PhD_2019\\Spatial_Datasets\\Baroon_Pocket_Dam_Catchment\\Raster\\Soil_Zonal_Stats')
for fname in fnames:
if fname.startswith(badprefix):
rename(fname, fname.replace(badprefix, '', 1))
and the following code as well:
import os
path = os.getcwd()
filenames = os.listdir(r'I:\PhD_2019\Spatial_Datasets\Baroon_Pocket_Dam_Catchment\Raster\Soil_Zonal_Stats' )
for filename in filenames:
os.rename(filename, filename.replace("Z_C", ""))
But both of them resulted in the same error!
Can anyone let me know what is wrong with my code, please?
You need to specify the full path to the file, otherwise python would look in your working directory and won't find the file. the fix is:
folder = r'I:\PhD_2019\Spatial_Datasets\Baroon_Pocket_Dam_Catchment\Raster\Soil_Zonal_Stats'
filenames =
os.listdir(r'I:\PhD_2019\Spatial_Datasets\Baroon_Pocket_Dam_Catchment\Raster\Soil_Zonal_Stats')
for filename in filenames:
os.rename(os.path.join(folder, filename), os.path.join(folder, filename.replace("Z_C","")))
I want to change the file names and folder names in a given directory and all subfolders. My folder structure is as follows:
top directory
file1
file2
folder1
file1
file2
file3
file4
folder2
file1
file2
file3
file4
I get the following error when executing the code below. I already checked the forums, but couldn't find a solution. Could someone please help me solve this issue and let me know what I need to do to get the program working?
Or is there a better solution for renaming files and folders in a tree?
Error message
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Filename 1' -> 'filename_1'
Code
#Change file names and folder names in a given directory and all its
subfolders
import os
os.chdir("path\\to\\folder")
print(os.getcwd())
#Walk the directory and change the file names and folder names in all folders and subfolders.
for root, dirs, files in os.walk("path\\to\\folder"):
for dir_name in dirs:
os.rename(dir_name, dir_name.replace(" ", "_").lower())
for file_name in files:
os.rename(file_name, file_name.replace(" ", "_").lower())
#Print done once all files have been renamed.
print("done")
You need to use root otherwise the rename can't find the path:
for root, dirs, files in os.walk("path/to/folder"):
for name in dirs + files:
os.rename(os.path.join(root, name), os.path.join(root, name.replace(" ", "_").lower()))
could it be that you're walking on a folder while renaming it so that it can't be found?
looks like you first need to rename the files and only then the dirs (and even then - make sure it's bottom up)
Try to change the filename first, otherwise you'll change the dir_name and lose reference.
Following solution works most of the time, still issues like same name files may exist after normalizing the name.
import os
os.chdir("path/to/dir")
print(os.getcwd())
#Walk the directory and change the file names and folder names in all folders and subfolders.
for root, dirs, files in os.walk("path/to/dir", topdown=False):
for file_name in files:
new_name = file_name.replace(" ", "_").lower()
if (new_name != file_name):
os.rename(os.path.join(root, file_name), os.path.join(root, new_name))
for dir_name in dirs:
new_name = dir_name.replace(" ", "_").lower()
if (new_name != dir_name):
os.rename(os.path.join(root, dir_name), os.path.join(root, new_name))
Here I copied all files inside of each subfolder to another path.
First use os.listdir() to move through each folder, then use it to move through files which are inside of the folder path. Finally use os.rename() to rename file name. Here, I changed file name to folder name_file name which is "folder_file":
path = 'E:/Data1'
path1 = 'E:/Data2'
for folder in os.listdir(path):
for file in os.listdir(path + '/'+ folder):
src = path + '/' + folder + '/' + file
des = path1 + '/' + folder + '_' +file
os.rename(src, des)
I would like to:
Write a script that takes a single directory path as command line argument, and then walks all subdirectories of that path looking for files with the extension '.py', copying each one to a temporary directory in your file system (eg /tmp/pyfiles). Your script should check for the existence of the temporary directory, and remove it if it already exists; it should then create a new directory, before beginning to copy files.
I have this:
#!/usr/bin/env python
import os, sys
import shutil
#import module
rootdir = sys.argv[1]
#take input directory
if os.path.exists('tmp/pyfiles'):
shutil.rmtree('tmp/pyfiles')
if not os.path.exists('tmp/pyfiles'):
os.makedirs('tmp/pyfiles')
#check whether directory exists, if it exists remove and remake, if not make
for root, dirs, files in os.walk(rootdir):
for f in files:
if os.path.splitext(f)[1] in ['.py']:
shutil.copy2(f, tmp/pyfiles)
#find files ending with .py, copy them and place in tmp/pyfiles directory
I get this error:
Traceback (most recent call last):
File "seek.py", line 20, in <module>
shutil.copy2(f, tmp/pyfiles)
NameError: name 'tmp' is not defined
Could anyone help me out?:) Thx
Your code says shutil.copy2(f, tmp/pyfiles), I believe it meant to be
shutil.copy2(f, 'tmp/pyfiles').
When you use the
os.walk()
method you loose track of the file full path. What I would do is to analyze each directory using the
os.listdir()
method and then copying each file taking into account its absolute path. Something like this:
for root, dirs, files in os.walk(rootdir):
for dir in dirs:
for f in os.listdir(dir):
if os.path.splitext(f)[1] in ['.py']:
shutil.copy2(os.path.join(root, dir, f), "tmp/pyfiles")
I hope this helps, maybe there is a cleaner solution.
You have to check if the root directory exists and walk in to remove everything if not create a new one.
To copy from dir to yours you have to check for files in dir that filename ends with .py, then replace the dir path with root path and create a new file in root dir with the content of matching file.
If we found a directory in dir we should create a new one in the root directory.
After that just call the function recursively to copy all content of dir to the root directory
import os, sys
rootdir = sys.argv[1]
PATH = "/tmp/pyfiles/"
def check(path, _file):
global rootdir, PATH
for item in os.listdir(path):
newpath = os.path.join(path, item)
if os.path.isdir(newpath):
os.mkdir(os.path.join(PATH, newpath.replace(rootdir, '')))
check(newpath, _file)
else:
if item.endswith(_file):
source = open(newpath, 'r')
print os.path.join(path, newpath.replace(rootdir, ''))
output = open(os.path.join(PATH, newpath.replace(rootdir, '')), 'w')
output.write(source.read())
output.close()
source.close()
if __name__ == '__main__':
if os.path.isdir(PATH):
for root, dirs, files in os.walk(PATH, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(PATH)
os.mkdir(PATH)
check(rootdir, '.py')