I have a folder contains both zipped and non zipped files, I want to migrate data in a given format from one place to another in the local mode.
I am new in Python so I tried os module for the first time and for accessing all the files in the folder, I am using os.walk for root path, directory list and file lists. It is showing that zipped files are not directories and normal files are directories.
When I am printing root path then it is showing in this way
Desktop/shin/archieve
Desktop/shin/archieve\New folder
My code is
for path,dir_list,file_list in os.walk('Desktop/shin/archieve'):
print(path)
I am confused about "Desktop/shin/archieve\New folder" and "Desktop/shin/archieve/New folder" and also why zipped files are considering as files only.
Related
I have multiple folders. I will read some of them; each folder includes various files (images) that I want access to their paths. I used the below code:
[os.path.join(folder,fname) for fname in os.listdir(folder) for folder in selected_train]
previously I had a list of folders in folders.
but I get the below error:
name 'folder' is not defined
How can I correct it?
You can use os.walk function to navigate through the folder.
import os
subfiles=[x[2] for x in os.walk(".")]
print(subfiles)
And also, if you want to just one list you can do this;
import os
subfiles=[]
for x in os.walk("."):
subfiles.extend(x[2])#x[2] represent the all files in one directory
print(subfiles)
I am looking to write a piece of python code that deletes all folders and their contents, but does not delete individual files.
For example here are some files and folders contained in a directory (Folder B) along with the script file that does the deleting. How do I delete folderA, folderB,folderC,etc, but leave the files? Thanks
/Folder B
file.docx
fileB.docx
fileC.docx
pythonDeleteScript.py
folderA/
folderB/
folderC/
folderD/
Use os.listdir() to get the contents of the directory, os.path.isdir(path) to see if it is a folder, and if it is, shutil.rmtree(path) to delete the folder and all its content.
I have several folders (let's say 10 folders) each of which has several subfolders.
I want to search each one of the folders in root and find the files which contain RGB in their filename and copy them into a folder.
the result would be like 10 folders with files that just have RGB in their filenames.
Use os.walk to iterate through the folders
Get file name having RGB along with the parent folder name
Use the join function to create the new location and copy the file to the location
I have an html parser function and it needs absolute path for each file.
How do I search a directory and find only files ending with .html and then return the absolute path of each file?
Have you considered using the python os module? It has the listdir(path) command
os.listdir(path)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Availability: Unix, Windows.
Use it to get all the files names in your directory, filter out the non-html files, and then prepend the path to the directory to get the absolute path.
https://docs.python.org/2/library/os.html
I'm having trouble making folders that I create go where I want them to go. For each file in a given folder, I want to create a new folder, then put that file in the new folder. My problem is that the new folders I create are being put in the parent directory, not the one I want. My example:
def createFolder():
dir_name = 'C:\\Users\\Adrian\\Entertainment\\Coding\\Test Folder'
files = os.listdir(dir_name)
for i in files:
os.mkdir(i)
Let's say that my files in that directory are Hello.txt and Goodbye.txt. When I run the script, it makes new folders for these files, but puts them one level above, in 'C:\Users\Adrian\Entertainment\Coding.
How do I make it so they are created in the same place as the files, AKA 'C:\Users\Adrian\Entertainment\Coding\Test Folder'?
import os, shutil
for i in files:
os.mkdir(os.path.join(dir_name , i.split(".")[0]))
shutil.copy(os.path.join(dir_name , i), os.path.join(dir_name , i.split(".")[0]))
os.listdir(dir_name) lists only the names of the files, not full paths to the files. To get a path to the file, join it with dir_name:
os.mkdir(os.path.join(dir_name, i))