How to create a directory? - python

I tried using open but it gives an error that the folder doesn't exist, which makes no sense since this is a command to create a folder, not read one. I saw Automatically creating directories with file output, but there is an error saying this is a Errno 30: Read only system: "/folder". Does anyone know how to avoid Error 30?
My code so far:
import os
filename = "/folder/y.txt"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write("FOOBAR")

I figured i just shouldn't put the slash behind the "folder"filename = "folder/y.txt" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "w") as f: f.write("FOOBAR")

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".

How open file based on extension?

I want to open any .txt file in the same directory.
In ruby I can do
File.open("*.txt").each do |line|
puts line
end
In python I can't do this it will give an error
file = open("*.txt","r")
print(file.read())
file.close()
It gives an error invalid argument.
So is there any way around it?
You can directly use the glob module for this
import glob
for file in glob.glob('*.txt'):
with open(file, 'r') as f:
print(f.read())
Use os.listdir to list all files in the current directory.
all_files = os.listdir()
Then, filter the ones which have the extension you are looking for and open each one of them in a loop.
for filename in all_files:
if filename.lower().endswith('.txt'):
with open(filename, 'rt') as f:
f.read()

Copying all files of a directory to one text file in python

My intention is to copy the text of all my c# (and later aspx) files to one final text file, but it doesn't work.
For some reason, the "yo.txt" file is not created.
I know that the iteration over the files works, but I can't write the data into the .txt file.
The variable 'data' eventually does contain all text from the files . . .
*******Could it be connected to the fact that there are some non-ascii characters in the text of the c# files?
Here is my code:
import os
import sys
src_path = sys.argv[1]
os.chdir(src_path)
data = ""
for file in os.listdir('.'):
if os.path.isfile(file):
if file.split('.')[-1]=="cs" and (len(file.split('.'))==2 or len(file.split('.'))==3):
print "Copying", file
with open(file, "r") as f:
data += f.read()
print data
with open("yo.txt", "w") as f:
f.write(data)
If someone has an idea, it will be great :)
Thanks
You have to ensure the directory the file is created has sufficient write permissions, if not run
chmod -R 777 .
to make the directory writable.
import os
for r, d, f in os.walk(inputdir):
for file in f:
filelist.append(f"{r}\\{file}")
with open(outputfile, 'w') as outfile:
for f in filelist:
with open(f) as infile:
for line in infile:
outfile.write(line)
outfile.write('\n \n')

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()

Saving Files In New Directory (python)

I'm trying to take the input file and save it into a new folder on my computer, but I can't figure out how to do it correctly.
Here is the code I tried:
from os.path import join as pjoin
a = raw_input("File Name: ")
filepath = "C:\Documents and Settings\User\My Documents\'a'"
fout = open(filepath, "w")
path_to_file = pjoin("C:\Documents and Settings User\My Documents\Dropbox",'a')
FILE = open(path_to_file, "w")
When I run it, it's putting two \ in between each sub-directory instead of one and it's telling me it's not an existing file or directory.
I am sure there is an easier way to do this, please help.
Why do you have unescaped "'quotes_like_this_inside_quotes'"? That may be a reason for that failure.
From what I can understand, the directories you are saving to are "C:\Documents and Settings\User\My Documents\' and 'C:\Documents and Settings\User\My Documents\'.
Whenever you are messing with directories/paths ALWAYS use os.expanduser('~/something/blah').
Try this:
from os.path import expanduser, join
path_to_file1 = join(expanduser('~/Dropbox/'), 'a')
path_to_file2 = join(expanduser('~'), 'a')
fout = open(path_to_file2, "w")
FILE = open(path_to_file1, "w")
And the double-backslashes are OK, AFAIK.
Let me know if this works - I'm not on a Windows box at the moment.

Categories