OSError sometimes occurs when opening .txt files in python
I'm trying to make a program that takes in the words of a .txt file then creates a new .txt file containing only the words with more than 5 letters. To do this I have the with open
command twice. Here is the code I have so far.
iteration=0
while iteration < 101:
with open(r"C:UsersuserDocumentsfilename.txt", "r") as file1:
inputwords=file1.readlines()[1]
wordtest=list(inputwords)
wordstr=''.join(wordtest)
words=len(wordtest)
if words>=5:
print(wordstr + " is longer then 5 letters")
with open(r"C:UsersuserDesktopnewfile.txt", "a") as file:
file.write("n" +wordstr)
iteration+=1
When I run it gives me an OSError but when I remove the first with open
it doesn't give me an OSError and works fine. I'm using Windows 10 and the error message is:
OSError: [Errno 22] Invalid argument: 'u202aC:UsersuserDocumentsfilename.txt'
Also I have attempted using double back-slashes and I get the same error. This has happened in other scripts and in one case every few times I attempted opening the script it worked fine, other times it returned the OSError
python
|
show 1 more comment
I'm trying to make a program that takes in the words of a .txt file then creates a new .txt file containing only the words with more than 5 letters. To do this I have the with open
command twice. Here is the code I have so far.
iteration=0
while iteration < 101:
with open(r"C:UsersuserDocumentsfilename.txt", "r") as file1:
inputwords=file1.readlines()[1]
wordtest=list(inputwords)
wordstr=''.join(wordtest)
words=len(wordtest)
if words>=5:
print(wordstr + " is longer then 5 letters")
with open(r"C:UsersuserDesktopnewfile.txt", "a") as file:
file.write("n" +wordstr)
iteration+=1
When I run it gives me an OSError but when I remove the first with open
it doesn't give me an OSError and works fine. I'm using Windows 10 and the error message is:
OSError: [Errno 22] Invalid argument: 'u202aC:UsersuserDocumentsfilename.txt'
Also I have attempted using double back-slashes and I get the same error. This has happened in other scripts and in one case every few times I attempted opening the script it worked fine, other times it returned the OSError
python
1
Is the secondwith open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?
– Michael Butscher
Nov 20 '18 at 23:09
3
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
1
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
1
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the"C
at the start of the string, delete it, then retype those two characters.
– jasonharper
Nov 22 '18 at 5:54
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (u202a
) prior to theC
– donkopotamus
Nov 22 '18 at 6:18
|
show 1 more comment
I'm trying to make a program that takes in the words of a .txt file then creates a new .txt file containing only the words with more than 5 letters. To do this I have the with open
command twice. Here is the code I have so far.
iteration=0
while iteration < 101:
with open(r"C:UsersuserDocumentsfilename.txt", "r") as file1:
inputwords=file1.readlines()[1]
wordtest=list(inputwords)
wordstr=''.join(wordtest)
words=len(wordtest)
if words>=5:
print(wordstr + " is longer then 5 letters")
with open(r"C:UsersuserDesktopnewfile.txt", "a") as file:
file.write("n" +wordstr)
iteration+=1
When I run it gives me an OSError but when I remove the first with open
it doesn't give me an OSError and works fine. I'm using Windows 10 and the error message is:
OSError: [Errno 22] Invalid argument: 'u202aC:UsersuserDocumentsfilename.txt'
Also I have attempted using double back-slashes and I get the same error. This has happened in other scripts and in one case every few times I attempted opening the script it worked fine, other times it returned the OSError
python
I'm trying to make a program that takes in the words of a .txt file then creates a new .txt file containing only the words with more than 5 letters. To do this I have the with open
command twice. Here is the code I have so far.
iteration=0
while iteration < 101:
with open(r"C:UsersuserDocumentsfilename.txt", "r") as file1:
inputwords=file1.readlines()[1]
wordtest=list(inputwords)
wordstr=''.join(wordtest)
words=len(wordtest)
if words>=5:
print(wordstr + " is longer then 5 letters")
with open(r"C:UsersuserDesktopnewfile.txt", "a") as file:
file.write("n" +wordstr)
iteration+=1
When I run it gives me an OSError but when I remove the first with open
it doesn't give me an OSError and works fine. I'm using Windows 10 and the error message is:
OSError: [Errno 22] Invalid argument: 'u202aC:UsersuserDocumentsfilename.txt'
Also I have attempted using double back-slashes and I get the same error. This has happened in other scripts and in one case every few times I attempted opening the script it worked fine, other times it returned the OSError
python
python
edited Nov 22 '18 at 5:41
SadDuck
asked Nov 20 '18 at 23:06
SadDuckSadDuck
32
32
1
Is the secondwith open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?
– Michael Butscher
Nov 20 '18 at 23:09
3
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
1
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
1
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the"C
at the start of the string, delete it, then retype those two characters.
– jasonharper
Nov 22 '18 at 5:54
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (u202a
) prior to theC
– donkopotamus
Nov 22 '18 at 6:18
|
show 1 more comment
1
Is the secondwith open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?
– Michael Butscher
Nov 20 '18 at 23:09
3
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
1
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
1
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the"C
at the start of the string, delete it, then retype those two characters.
– jasonharper
Nov 22 '18 at 5:54
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (u202a
) prior to theC
– donkopotamus
Nov 22 '18 at 6:18
1
1
Is the second
with open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?– Michael Butscher
Nov 20 '18 at 23:09
Is the second
with open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?– Michael Butscher
Nov 20 '18 at 23:09
3
3
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
1
1
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
1
1
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the
"C
at the start of the string, delete it, then retype those two characters.– jasonharper
Nov 22 '18 at 5:54
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the
"C
at the start of the string, delete it, then retype those two characters.– jasonharper
Nov 22 '18 at 5:54
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (
u202a
) prior to the C
– donkopotamus
Nov 22 '18 at 6:18
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (
u202a
) prior to the C
– donkopotamus
Nov 22 '18 at 6:18
|
show 1 more comment
1 Answer
1
active
oldest
votes
Might be the issue with the file path you're passing in.
as per your code you're passing the path as
with open(r"C:Users\Documentsfilename.txt", "r") as file1:
if you're passing the raw string as path then simply give the path in the statement as below instead of having double backslash.
with open(r"C:UsersDocumentsfilename.txt", "r") as file1:
or you can simply use single slash
c:/users/path
or double backslash (given twice to avoid the special meaning of backslash as an escape sequence)
c:\users\path
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402953%2foserror-sometimes-occurs-when-opening-txt-files-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Might be the issue with the file path you're passing in.
as per your code you're passing the path as
with open(r"C:Users\Documentsfilename.txt", "r") as file1:
if you're passing the raw string as path then simply give the path in the statement as below instead of having double backslash.
with open(r"C:UsersDocumentsfilename.txt", "r") as file1:
or you can simply use single slash
c:/users/path
or double backslash (given twice to avoid the special meaning of backslash as an escape sequence)
c:\users\path
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
add a comment |
Might be the issue with the file path you're passing in.
as per your code you're passing the path as
with open(r"C:Users\Documentsfilename.txt", "r") as file1:
if you're passing the raw string as path then simply give the path in the statement as below instead of having double backslash.
with open(r"C:UsersDocumentsfilename.txt", "r") as file1:
or you can simply use single slash
c:/users/path
or double backslash (given twice to avoid the special meaning of backslash as an escape sequence)
c:\users\path
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
add a comment |
Might be the issue with the file path you're passing in.
as per your code you're passing the path as
with open(r"C:Users\Documentsfilename.txt", "r") as file1:
if you're passing the raw string as path then simply give the path in the statement as below instead of having double backslash.
with open(r"C:UsersDocumentsfilename.txt", "r") as file1:
or you can simply use single slash
c:/users/path
or double backslash (given twice to avoid the special meaning of backslash as an escape sequence)
c:\users\path
Might be the issue with the file path you're passing in.
as per your code you're passing the path as
with open(r"C:Users\Documentsfilename.txt", "r") as file1:
if you're passing the raw string as path then simply give the path in the statement as below instead of having double backslash.
with open(r"C:UsersDocumentsfilename.txt", "r") as file1:
or you can simply use single slash
c:/users/path
or double backslash (given twice to avoid the special meaning of backslash as an escape sequence)
c:\users\path
answered Nov 21 '18 at 9:37
Sekar RamuSekar Ramu
164110
164110
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
add a comment |
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
The double backslash is from where I removed the user area. It had my name in it and I didn't want that there. Sorry if I misunderstood your answer.
– SadDuck
Nov 22 '18 at 0:08
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
when you are passing your path in r' ' it will take it as a raw string and hence no escape characters are needed. And after you edit to the question could see a invisible unicode character being pasted. To avoid that instead of copying the path from the dialogue box just type the same manually. That will avoid that Unicode character issue. Here you can simple delete the line and type the entire line manually.
– Sekar Ramu
Nov 22 '18 at 6:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402953%2foserror-sometimes-occurs-when-opening-txt-files-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is the second
with open
nested inside the first or after it? Opens it the same file? Which OS? Is there a more detailed error message (error number)?– Michael Butscher
Nov 20 '18 at 23:09
3
We need the full code (with dummy variables is fine) and the full error traceback to help you
– G. Anderson
Nov 20 '18 at 23:13
1
Welcome to Stack Overflow! Please read what this site is about and "How to ask" before asking a question.
– Sean Pianka
Nov 20 '18 at 23:16
1
You have an invisible Unicode control character in your pathname string, which was likely pasted from somewhere else. There's an explanation of how this might happen at blogs.msdn.microsoft.com/oldnewthing/20150506-00/?p=44924 To fix, select the
"C
at the start of the string, delete it, then retype those two characters.– jasonharper
Nov 22 '18 at 5:54
@jasonharper is correct ... the traceback is very clear about the fact that there is a Unicode character in the file name (
u202a
) prior to theC
– donkopotamus
Nov 22 '18 at 6:18