This question already has an answer here:
Outputting Japanese Characters to a File using Python
(1 answer)
Closed 4 years ago.
How can I print Turkish?
open("save.txt", "w+").write("C:\Müzikler")
folder = open("save.txt", "r+").read()
os.listdir(folder)
OUTPUT:
FileNotFoundError: [WinError 3] The system cannot find the path specified:
C:\xfczikler'
For special characters (or in your case, accents) you need to encode with utf-8.
Do this with:
open("save.txt", 'w+', encoding='utf-8').write("C:\Müzikler")
Related
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
I just started programming in python yesterday.
Ultimately I want to open a comma deliminated file and read its contents. I searched for related topics and tried using some of the code and am running into some errors.
Code:
def ReadTxtFile():
fname="c:\vba\lapseC2.csv"
#with open(fname) as f:
with open("c:\vba\lapseC2.csv", "r") as f:
content = f.readlines
you may also want to remove whitespace characters like \n at the end of each line
content = [x.strip() for x in content]
ReadTxtFile()
Error:
OSError: [Errno 22] Invalid argument: 'c:\x0bba\lapseC2.csv'
The backslash works as an escape symbol so if you want to insert it as a part of string, you should escape backslash itself:
fname="c:\\vba\\lapseC2.csv"
Another option is to use slashes, which seem to be supported by most popular operating systems:
fname="c:/vba/lapseC2.csv"
This question already has answers here:
Why do numbers in a string become "x0n" when a backslash precedes them?
(2 answers)
Closed 6 years ago.
I'm tring to open a file using the following code:
f=open('C:\Users\gabor\Desktop\NPI\test.csv', 'r')
reader=csv.reader(f)
for row in reader:
print row
Its returning an error:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\gabor\\Desktop\\NPI\test.csv'
I've change 'rb' to 'r' and left it out and I keep getting the same error.
Any suggestions on how to open the file?
I think you need double back-slash in your path. Or you can put "r" before the string as:
f=open(r'C:\Users\gabor\Desktop\NPI\test.csv', 'r').
This question already has answers here:
How to empty a file using Python
(2 answers)
Closed 6 years ago.
I'm confused and don't know what to do about this. I'm trying to overwrite the files text.
when opening a file with 'w' flag, it will rewrite the file if it exists.
with open('yourfile.ext', 'wt') as fileObj:
fileObj.write(stuff)
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 6 months ago.
I am an infant in the coding world (week 6) and I need some help! My overall goal is to write a program that inputs the file unsorted_fruits.tex, reads it, sorts the list alphabetically, and then writes it out to a file called sorted_fruits.txt.
So far I have my basics (aside from sorting and writing it into the new file)
infile=open("unsorted_fruits.tex", "r")
outfile=open("sorted_fruits.txt","w")
fruit=infile.read(26)
outfile.write(fruit)
unsorted_fruits.sort()
print (fruit)
infile.close()
outfile.close()
However I keep getting the [Errno 2] No such file or directory: 'unsorted_fruits.tex'
The file is definitely saved to my computer. I thought it might be .tex (I wasn't familiar with this format) so I changed the file to .txt. and called the .txt to see if that worked, no luck, so I changed it back to .tex
Any help is appreciated, thanks!!
Your code tries to find the text file in current directory. (For example, it can be the directory where Python interpreter is installed). So you may want to specify the absolute path. if text files are in the same dir with python script, you may want to use something like:
file_name = os.path.join(os.path.dirname(__file__), 'unsorted_fruits.tex')
with open(file_name, 'r') as f:
data = f.read()
(Note: I'm using "with" syntax while working with file, so I do not need to close it manually)
This question already has answers here:
Create empty file using python [duplicate]
(2 answers)
Closed 8 years ago.
This is the code I have so far, yes I know the 'dirs' code creates a folder, but I am not sure of the code (if there is one) to create a file instead!
import os
if os.path.isfile(outfile):
print("Name already used, please choose another")
else:
os.makedirs(outfile)
Any help would be appreciated :D
Inorder to create a file and work on it, use the open() function.
fp = open(outfile, mode)
modes:
r: read
w: if file exists, replace it with a new one
a: append existing file
In your case the mode is 'w'
For create a file you can just open a file in write mode
open('file.txt', 'w')
https://docs.python.org/3/library/functions.html#open