Python Bulk Blank folder creation - python

i am trying to create bulk folders based on simple text file. os.makedir helps to create new folder but i am not sure how to incorporate with newpath variable along with folder list. following is what i am trying with. I understand that code has syntax error. So need some help to correct/enhance the code.
import os.path
newpath = r'C:\Program Files\test\'
with open('folders.txt') as f:
for line in f:
ff = os.makedirs(newpath,line.strip())
ff.close()

Use os.path.join to join path components.
import os.path
newpath = r'C:\Program Files\test\'
with open('folders.txt') as f:
for line in f:
os.makedirs(os.path.join(newpath, line.strip()))

You can use os.path.join function documented here.

Perhaps something like this?
import os, sys
newpath = 'C:\Program Files\test'
with open(open('folders.txt') as f:
for line in f:
newdir = os.path.join(newpath, line.strip())
try:
os.makedirs(newdir)
except OSError: # if makedirs() failed
sys.stderr.write("ERR: Could not create %s\n" % newdir)
pass # continue with next line
Notes:
Use os.path.join() to combine a paths. This will automatically use separators that are suitable for your OS.
os.makedirs() does not return anything
os.makedirs() will raise an OSError exception if the directory already exists or cannot be created.

Related

Python: Unicode characters in file or folder names

We process a lot of files where path can contain an extended character set like this:
F:\Site Section\Cieślik
My Python scripts fail to open such files or chdir to such folders whatever I try.
Here is an extract from my code:
import zipfile36 as zipfile
import os
from pathlib import Path
outfile = open("F:/zip_pdf3.log", "w", encoding="utf-8")
with open('F:/zip_pdf.txt') as f: # Input file list - note the forward slashes!
for line in f:
print (line)
path, filename = os.path.split(line)
file_no_ext = os.path.splitext(os.path.basename(line))[0]
try:
os.chdir(path) # Go to the file path
except Exception as exception:
print (exception, file = outfile) #3.7
print (exception)
continue
I tried the following:
Converting path to a raw string
raw_string = r"{}".format(path)
try:
os.chdir(raw_string)
Converting a string to Path
Ppath = Path(path)
try:
os.chdir(Ppath.decode("utf8"))
Out of ideas... Anyone knows how to work with Unicode file and folder names? Using Python 3.7 or higher on Windows.
Could be as simple as that - thanks #SergeBallesta:
with open('F:/pdf_err.txt', encoding="utf-8") as f:
I may post updates after more runs with different input.
This, however, leads to a slightly different question: if, instead of reading from the file, I walk over folders and files with extended character set - how do I deal with those, i.e.
for subdir, dirs, files in os.walk(rootdir): ?
At present I'm getting either a "The filename, directory name, or volume label syntax is incorrect" or "Can't open the file".

Python error: FileNotFoundError: [Errno 2] No such file or directory

I am trying to open the file from folder and read it but it's not locating it. I am using Python3
Here is my code:
import os
import glob
prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-
codes/Mayur_Python_code/Question/wx_data/"
target_path = open('MissingPrcpData.txt', 'w')
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if
f.endswith('.txt')]
file_array.sort() # file is sorted list
for f_obj in range(len(file_array)):
file = os.path.abspath(file_array[f_obj])
join_file = os.path.join(prefix_path, file) #whole file path
for filename in file_array:
log = open(filename, 'r')#<---- Error is here
Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'
You are not giving the full path to a file to the open(), just its name - a relative path.
Non-absolute paths specify locations in relation to current working directory (CWD, see os.getcwd).
You would have to either os.path.join() correct directory path to it, or os.chdir() to the directory that the files reside in.
Also, remember that os.path.abspath() can't deduce the full path to a file just by it's name. It will only prefix its input with the path of the current working directory, if the given path is relative.
Looks like you are forgetting to modify the the file_array list. To fix this, change the first loop to this:
file_array = [os.path.join(prefix_path, name) for name in file_array]
Let me reiterate.
This line in your code:
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if f.endswith('.txt')]
is wrong. It will not give you a list with correct absolute paths. What you should've done is:
import os
import glob
prefix_path = ("C:/Users/mpotd/Documents/GitHub/Python-Sample-"
"codes/Mayur_Python_code/Question/wx_data/")
target_path = open('MissingPrcpData.txt', 'w')
file_array = [f for f in os.listdir(prefix_path) if f.endswith('.txt')]
file_array.sort() # file is sorted list
file_array = [os.path.join(prefix_path, name) for name in file_array]
for filename in file_array:
log = open(filename, 'r')
You are using relative path where you should be using an absolute one. It's a good idea to use os.path to work with file paths. Easy fix for your code is:
prefix = os.path.abspath(prefix_path)
file_list = [os.path.join(prefix, f) for f in os.listdir(prefix) if f.endswith('.txt')]
Note that there are some other issues with your code:
In python you can do for thing in things. You did for thing in range(len(things)) it's much less readable and unnecessary.
You should use context managers when you open a file. Read more here.

How to retrieve the path of files from a folder into a text file with a python script?

import shutil
import os
def get_files():
source = os.listdir("/output_folder/sample/cufflinks/")
destination = "output_folder/assemblies.txt"
for files in source:
if files.with("transcripts.gtf"):
shutil.move(files,destination)
I want to retrieve transcripts.gtf files from "/output_folder/sample/cufflinks/" to assemblies.txt. Is the above code correct or not. Please help me out. Thank you !!
You can use os.walk :
import os
import shutil
from os.path import join
destination = "/output_folder/assemblies.txt" # Absolute path, or use join
folder_to_look_in = "/output_folder/sample/cufflinks/" # Absolute folder path, or use join
for _, _, files in os.walk(folder_to_look_in):
for file_name in files:
if file_name.endswith("transcripts.gtf"):
try:
# 'a' will append the data to the tail of the file
with open(destination, 'a') as my_super_file:
my_super_file.write(file_name)
except OSError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
I think I finally understand what you are trying to achieve. Use the glob.glob() function:
import glob
destination = "output_folder/assemblies.txt"
source = '/output_folder/sample/cufflinks/*/*transcripts.gtf'
with open(destination, 'w') as f:
f.write('\n'.join(glob.glob(source))+'\n')
f.write('\n'.join(glob.glob(source))+'\n') is the equivalent of:
s = ''
for path in glob.glob(source):
s += '{}\n'.format(path)
f.write(s)

Opening a file in a folder in Python?

I want to open a file to write to.
with open(oname.text , 'w') as f:
and now I want to write the files in a folder "Playlist"
I know that I have to use os.path But I do not know how to use it
ty all
path = os.path.join('Playlist', oname.text)
with open(path, 'w') as f:
...
If you're not sure if the 'Playlist' subdir of the current directory already exists, prefix that with:
if not os.path.isdir('Playlist'):
if os.path.exists('Playlist'):
raise RuntimeError('Playlist exists and is a file, now what?!')
os.mkdir('Playlist')
This raises an exception if 'Playlist' does exist but as a file, not a directory -- handle this anomalous case as you wish, but unless you remove or rename the file, you're not going to be able to have it as a directory as well!
Use os.makedirs instead of os.mkdir if the path you desire has multiple levels of directories, e.g Play/List/Whatever (you could use it anyway just in case).
You could change the current working directory using os.chdir function.
os.chdir('Playlist')
with open(oname.text , 'w') as f:
...
Use with statement and os.path.join method
dir_path = "/home/Playlist"
file_path = os.path.join('dir_path, "oname.txt")
content = """ Some content..."""
with open(file_path, 'wb') as fp:
fp.write(content)
OR
fp = open(file_path, "wb"):
fp.write(content)
fp.close()

Deleting files which start with a name Python

I have a few files I want to delete, they have the same name at the start but have different version numbers. Does anyone know how to delete files using the start of their name?
Eg.
version_1.1
version_1.2
Is there a way of delting any file that starts with the name version?
Thanks
import os, glob
for filename in glob.glob("mypath/version*"):
os.remove(filename)
Substitute the correct path (or . (= current directory)) for mypath. And make sure you don't get the path wrong :)
This will raise an Exception if a file is currently in use.
If you really want to use Python, you can just use a combination of os.listdir(), which returns a listing of all the files in a certain directory, and os.remove().
I.e.:
my_dir = # enter the dir name
for fname in os.listdir(my_dir):
if fname.startswith("version"):
os.remove(os.path.join(my_dir, fname))
However, as other answers pointed out, you really don't have to use Python for this, the shell probably natively supports such an operation.
In which language?
In bash (Linux / Unix) you could use:
rm version*
or in batch (Windows / DOS) you could use:
del version*
If you want to write something to do this in Python it would be fairly easy - just look at the documentation for regular expressions.
edit:
just for reference, this is how to do it in Perl:
opendir (folder, "./") || die ("Cannot open directory!");
#files = readdir (folder);
closedir (folder);
unlink foreach (grep /^version/, #files);
import os
os.chdir("/home/path")
for file in os.listdir("."):
if os.path.isfile(file) and file.startswith("version"):
try:
os.remove(file)
except Exception,e:
print e
The following function will remove all files and folders in a directory which start with a common string:
import os
import shutil
def cleanse_folder(directory, prefix):
for item in os.listdir(directory):
path = os.path.join(directory, item)
if item.startswith(prefix):
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(os.path.join(directory, item)):
shutil.rmtree(path)
else:
print("A simlink or something called {} was not deleted.".format(item))
import os
import re
directory = "./uploaded"
pattern = "1638813371180"
files_in_directory = os.listdir(directory)
filtered_files = [file for file in files_in_directory if ( re.search(pattern,file))]
for file in filtered_files:
path_to_file = os.path.join(directory, file)
os.remove(path_to_file)

Categories