I'm working with Python and I'd like to copy 3 files of a certain folder
/Users/jake/Desktop/exp to all the subfolders of other sub-directories belonging to the directory /toyspace:
/Users/jake/Desktop/toyspace/1A/AAA
/Users/jake/Desktop/toyspace/1A/BBB
/Users/jake/Desktop/toyspace/1A/CCC
/Users/jake/Desktop/toyspace/2B/AAA
/Users/jake/Desktop/toyspace/2B/BBB
/Users/jake/Desktop/toyspace/2B/CCC
So the subfolders names are the same for all the sub-directories. I wrote something like that:
from distutils.dir_util import copy_tree
def myfunc (source, destination):
fromDirectory = source
toDirectory = destination
copy_tree(fromDirectory, toDirectory)
for subfold in toDirectory:
myfunc(fromDirectory, subfold)
Where source =/Users/jake/Desktop/exp and destination =/Users/jake/Desktop/toyspace, but it returns me an error:
DistutilsFileError: could not create '/motif_list.txt': Read-only file system
Can you help me? Thanks in advance!
Unfortunately I have not used distutils but you can try to automate using the os command as below
import os
def copy_folders(source_dir, destination_dir):
files = os.listdir(source_dir)
for sub_folder1 in os.listdir(destination_dir):
for sub_folder in os.listdir(destination_dir + sub_folder1 + '/'):
for file in files:
os.system('cp ' + source_dir + file + ' ' + destination_dir + sub_folder1 + '/' + sub_folder + '/')
Let me know how it goes :)
Related
I am currently updating some files I have in a directory, with a loop, and I would like to save those file in a different directory.
Here is what I have:
from astropy.io import fits
from mpdaf.obj import Spectrum, WaveCoord
import os, glob
ROOT_DIR=input("Enter root directory : ")
os.chdir(ROOT_DIR)
destination=input("Enter destination directory : ")
fits_files = glob.glob("*.fits")
for spect in fits_files:
spe = Spectrum(filename= spect, ext=[0,1])
(spect_name, ext) = os.path.splitext(spect)
sperebin = spe.rebin(57)
sperebin.write(spect_name + "-" + "rebin" + ".fits")
With the last line sperebin.write(spect_name + "-" + "rebin" + ".fits") it is currently writing the file in the directory I'm in, and I would like it to write it directly into the destination directory, any idea how to proceed?
With os.path.join you can combine directory with filename to get the file path.
sperebin.write(os.path.join(destination, pect_name + "-" + "rebin" + ".fits"))
Also with os you can check if the directory exists and create it if you want to.
if not os.path.exists(destination):
os.makedirs(destination)
You don't need to or want to change directories in your script. Instead, use pathlib to simplify the creation of the new filename.
from pathlib import Path
root_dir = Path(input("Enter root directory : "))
destination_dir = Path(input("Enter destination directory : "))
# Spectrum wants a string for the filename argument
# so you need to call str on the Path object
for pth in root_dir.glob("*.fits"):
spe = Spectrum(filename=str(pth), ext=[0,1])
sperebin = spe.rebin(57)
dest_pth = destination_dir / pth.stem / "-rebin" / pth.suffix
sperebin.write(str(dest_pth))
I'm attempting to write a script that will save a file in the given directory, but I'm getting a NotADirecotryError[WinError 267] whenever I run it. Any ideas or tips on what I may have done incorrectly?
import shutil
import os
src = 'C:\\Users\\SpecificUsername\\Pictures\\test.txt\'
dest = 'C:\\Users\\SpecificUsername\\Desktop'
files = os.listdir(src)
for file in files:
shutil.copy(file, dest)
for file in files:
if os.path.isfile(file):
shutil.copy(file,dest) ```
There are a couple of things going on here:
You can just use forward slashes in the paths.
Your src is the test.txt file, and not a directory, so you cannot iterate over it using os.listdir().
You can also merge the two loops together since they are looping over the same set of data.
shutil.copy() takes a file path as input, while what you are passing is a filename.
The following code should work and it also copies directories as is:
import shutil
import os
basepath = "C:/Users/SpecificUsername/"
src = "Pictures/"
dest = "Desktop/"
files = os.listdir(os.path.join(basepath, src))
for filename in files:
filepath = os.path.join(basepath, src, filename)
if (os.path.isfile(filepath)):
print("File: " + filename)
shutil.copy(filepath,dest)
else:
print("Dir: " + filename)
shutil.copytree(filepath, os.path.join(dest, filename))
Hope it helps!
I've just run this script to rename my files, adding the 15 first chars of files, but now all the files are disappearead, and i can't find them. i've just run this on a mac
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
new_name = os.path.basename(path)[:15] + " - " + name
print path + "##" + new_name
os.rename(file_path, new_name)
replace('/Users/myuser/mp3/', '', '')
#DDave,
In the function call os.rename(src, dst): src is the fully qualified filepath of the source file, and dst is fully qualified filepath of the target file.
In your above program, new_name is just the new name of the file which does not have the directory information (and hence it is considering the directory as your current working directory). That is why you are not seeing the renamed files where you were looking to see them.
The program is rather trying to create the files under your current working directory. Run the following from your console of your IDE or from your program to figure out the current working directory:
print(os.getcwd())
Providing the full solution as requested in the comment below:
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
# new_name = os.path.basename(path)[:15] + " - " + name
new_filepath = os.path.join(path, (name[:15] if len(name)>=15 else name) + " - " + name)
print ("new path: " + new_filepath)
os.rename(file_path, new_filepath)
I am attempting to create a script that zips all of the subdirectories of a folder, then deletes the folders which have now been zipped
import shutil
import os
loc = "foldertzipfilesin"
path = "/whereparentis/" + loc + "/"
dirs = os.listdir( path )
for file in dirs:
name = file
shutil.make_archive(name, 'zip', path)
shutil.rmtree(name)
It seems to run, however it does not create the zip files.
Thanks
Probably your issue is with the path you're passing as an argument.
I've tried your script with the following changes:
import shutil
import os
loc = "testfolder"
path = os.getcwd() + '/' + loc + "/"
dirs = os.listdir( path )
for file in dirs:
name = file
shutil.make_archive(name, 'zip', path)
and it worked properly creating the zip files for every file in the loc folder. Files are created on the PATH where you've ran your script.
I'd like to browse through the current folder and all its subfolders and get all the files with .htm|.html extensions. I have found out that it is possible to find out whether an object is a dir or file like this:
import os
dirList = os.listdir("./") # current directory
for dir in dirList:
if os.path.isdir(dir) == True:
# I don't know how to get into this dir and do the same thing here
else:
# I got file and i can regexp if it is .htm|html
and in the end, I would like to have all the files and their paths in an array. Is something like that possible?
You can use os.walk() to recursively iterate through a directory and all its subdirectories:
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".html", ".htm")):
# whatever
To build a list of these names, you can use a list comprehension:
htmlfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(path)
for name in files
if name.endswith((".html", ".htm"))]
I had a similar thing to work on, and this is how I did it.
import os
rootdir = os.getcwd()
for subdir, dirs, files in os.walk(rootdir):
for file in files:
#print os.path.join(subdir, file)
filepath = subdir + os.sep + file
if filepath.endswith(".html"):
print (filepath)
Hope this helps.
In python 3 you can use os.scandir():
def dir_scan(path):
for i in os.scandir(path):
if i.is_file():
print('File: ' + i.path)
elif i.is_dir():
print('Folder: ' + i.path)
dir_scan(i.path)
Use newDirName = os.path.abspath(dir) to create a full directory path name for the subdirectory and then list its contents as you have done with the parent (i.e. newDirList = os.listDir(newDirName))
You can create a separate method of your code snippet and call it recursively through the subdirectory structure. The first parameter is the directory pathname. This will change for each subdirectory.
This answer is based on the 3.1.1 version documentation of the Python Library. There is a good model example of this in action on page 228 of the Python 3.1.1 Library Reference (Chapter 10 - File and Directory Access).
Good Luck!
Slightly altered version of Sven Marnach's solution..
import os
folder_location = 'C:\SomeFolderName'
file_list = create_file_list(folder_location)
def create_file_list(path):
return_list = []
for filenames in os.walk(path):
for file_list in filenames:
for file_name in file_list:
if file_name.endswith((".txt")):
return_list.append(file_name)
return return_list
There are two ways works for me.
1. Work with the `os` package and use `'__file__'` to replace the main
directory when the project locates
import os
script_dir = os.path.dirname(__file__)
path = 'subdirectory/test.txt'
file = os.path.join(script_dir, path)
fileread = open(file,'r')
2. By using '\\' to read or write the file in subfolder
fileread = open('subdirectory\\test.txt','r')
from tkinter import *
import os
root = Tk()
file = filedialog.askdirectory()
changed_dir = os.listdir(file)
print(changed_dir)
root.mainloop()