Why does ?? appear when I copy one text file to another?
When I copy one text file to another the new file has the two characters: (??) at the end which I do not want.
I am using Python3.6.0 on Windows7
This is my script:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
indata = open(from_file).read()
#Write the from_file text to to_file
open(to_file, 'w').write(indata)
I run the following in PowerShell:
>echo "This is a test file." > TestSource.txt
>type TestSource.txt
This is a test file.
>python CopyFile.py TestSource.txt TestDestination.txt
>type TestDestination.txt
This is a test file.??
Why do two question marks (??) appear in the file I have created?
Edit: This Related Question was suggested as duplicate. My question is about how Python behaves when I copy one text file to another. Where as this related question is about how Windows PowerShell creates a text file.
python python-3.x
add a comment |
When I copy one text file to another the new file has the two characters: (??) at the end which I do not want.
I am using Python3.6.0 on Windows7
This is my script:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
indata = open(from_file).read()
#Write the from_file text to to_file
open(to_file, 'w').write(indata)
I run the following in PowerShell:
>echo "This is a test file." > TestSource.txt
>type TestSource.txt
This is a test file.
>python CopyFile.py TestSource.txt TestDestination.txt
>type TestDestination.txt
This is a test file.??
Why do two question marks (??) appear in the file I have created?
Edit: This Related Question was suggested as duplicate. My question is about how Python behaves when I copy one text file to another. Where as this related question is about how Windows PowerShell creates a text file.
python python-3.x
1
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,open(to_file, 'w', encoding='ascii').write(indata)
– Nullman
Dec 30 '18 at 8:55
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
Thank you @Nullman. I triedopen(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).
– FergM
Dec 30 '18 at 9:12
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Apparently PowerShell is creating an UTF-16 file. Try to useencoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.
– Bakuriu
Dec 30 '18 at 9:59
add a comment |
When I copy one text file to another the new file has the two characters: (??) at the end which I do not want.
I am using Python3.6.0 on Windows7
This is my script:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
indata = open(from_file).read()
#Write the from_file text to to_file
open(to_file, 'w').write(indata)
I run the following in PowerShell:
>echo "This is a test file." > TestSource.txt
>type TestSource.txt
This is a test file.
>python CopyFile.py TestSource.txt TestDestination.txt
>type TestDestination.txt
This is a test file.??
Why do two question marks (??) appear in the file I have created?
Edit: This Related Question was suggested as duplicate. My question is about how Python behaves when I copy one text file to another. Where as this related question is about how Windows PowerShell creates a text file.
python python-3.x
When I copy one text file to another the new file has the two characters: (??) at the end which I do not want.
I am using Python3.6.0 on Windows7
This is my script:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
indata = open(from_file).read()
#Write the from_file text to to_file
open(to_file, 'w').write(indata)
I run the following in PowerShell:
>echo "This is a test file." > TestSource.txt
>type TestSource.txt
This is a test file.
>python CopyFile.py TestSource.txt TestDestination.txt
>type TestDestination.txt
This is a test file.??
Why do two question marks (??) appear in the file I have created?
Edit: This Related Question was suggested as duplicate. My question is about how Python behaves when I copy one text file to another. Where as this related question is about how Windows PowerShell creates a text file.
python python-3.x
python python-3.x
edited Jan 1 at 7:05
FergM
asked Dec 30 '18 at 8:48
FergMFergM
186
186
1
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,open(to_file, 'w', encoding='ascii').write(indata)
– Nullman
Dec 30 '18 at 8:55
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
Thank you @Nullman. I triedopen(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).
– FergM
Dec 30 '18 at 9:12
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Apparently PowerShell is creating an UTF-16 file. Try to useencoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.
– Bakuriu
Dec 30 '18 at 9:59
add a comment |
1
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,open(to_file, 'w', encoding='ascii').write(indata)
– Nullman
Dec 30 '18 at 8:55
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
Thank you @Nullman. I triedopen(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).
– FergM
Dec 30 '18 at 9:12
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Apparently PowerShell is creating an UTF-16 file. Try to useencoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.
– Bakuriu
Dec 30 '18 at 9:59
1
1
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,
open(to_file, 'w', encoding='ascii').write(indata)– Nullman
Dec 30 '18 at 8:55
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,
open(to_file, 'w', encoding='ascii').write(indata)– Nullman
Dec 30 '18 at 8:55
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
Thank you @Nullman. I tried
open(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).– FergM
Dec 30 '18 at 9:12
Thank you @Nullman. I tried
open(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).– FergM
Dec 30 '18 at 9:12
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Apparently PowerShell is creating an UTF-16 file. Try to use
encoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.– Bakuriu
Dec 30 '18 at 9:59
Apparently PowerShell is creating an UTF-16 file. Try to use
encoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.– Bakuriu
Dec 30 '18 at 9:59
add a comment |
1 Answer
1
active
oldest
votes
Powershell is creating the file using UTF-16. You have opened the file in text mode (default) without specifying an encoding, so python calls locale.getpreferredencoding(False) and uses that encoding (cp1252 on my US Windows system).
Text mode translates line endings and using the wrong encoding creates problems. To fix this, use binary mode to get a byte-for-byte copy regardless of encoding. I also suggest using with to ensure the files are closed properly:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
with open(from_file,'rb') as f:
data = f.read()
#Write the from_file text to to_file
with open(to_file,'wb') as f:
f.write(data)
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%2f53976302%2fwhy-does-appear-when-i-copy-one-text-file-to-another%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
Powershell is creating the file using UTF-16. You have opened the file in text mode (default) without specifying an encoding, so python calls locale.getpreferredencoding(False) and uses that encoding (cp1252 on my US Windows system).
Text mode translates line endings and using the wrong encoding creates problems. To fix this, use binary mode to get a byte-for-byte copy regardless of encoding. I also suggest using with to ensure the files are closed properly:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
with open(from_file,'rb') as f:
data = f.read()
#Write the from_file text to to_file
with open(to_file,'wb') as f:
f.write(data)
add a comment |
Powershell is creating the file using UTF-16. You have opened the file in text mode (default) without specifying an encoding, so python calls locale.getpreferredencoding(False) and uses that encoding (cp1252 on my US Windows system).
Text mode translates line endings and using the wrong encoding creates problems. To fix this, use binary mode to get a byte-for-byte copy regardless of encoding. I also suggest using with to ensure the files are closed properly:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
with open(from_file,'rb') as f:
data = f.read()
#Write the from_file text to to_file
with open(to_file,'wb') as f:
f.write(data)
add a comment |
Powershell is creating the file using UTF-16. You have opened the file in text mode (default) without specifying an encoding, so python calls locale.getpreferredencoding(False) and uses that encoding (cp1252 on my US Windows system).
Text mode translates line endings and using the wrong encoding creates problems. To fix this, use binary mode to get a byte-for-byte copy regardless of encoding. I also suggest using with to ensure the files are closed properly:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
with open(from_file,'rb') as f:
data = f.read()
#Write the from_file text to to_file
with open(to_file,'wb') as f:
f.write(data)
Powershell is creating the file using UTF-16. You have opened the file in text mode (default) without specifying an encoding, so python calls locale.getpreferredencoding(False) and uses that encoding (cp1252 on my US Windows system).
Text mode translates line endings and using the wrong encoding creates problems. To fix this, use binary mode to get a byte-for-byte copy regardless of encoding. I also suggest using with to ensure the files are closed properly:
from sys import argv
script, from_file, to_file = argv
#Open from_file and get the text from it
with open(from_file,'rb') as f:
data = f.read()
#Write the from_file text to to_file
with open(to_file,'wb') as f:
f.write(data)
edited Dec 30 '18 at 10:29
answered Dec 30 '18 at 10:23
Mark TolonenMark Tolonen
92.8k12112176
92.8k12112176
add a comment |
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%2f53976302%2fwhy-does-appear-when-i-copy-one-text-file-to-another%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
Welcome to SO. i have a suspicion that that last two characters aren't question marks, but something with encoding. can you check what their values are? also try saving the file with ascii encoding ,
open(to_file, 'w', encoding='ascii').write(indata)– Nullman
Dec 30 '18 at 8:55
How do I check what their values are? In notepad I see two boxes ഀ.
– FergM
Dec 30 '18 at 9:04
Thank you @Nullman. I tried
open(to_file, 'w', encoding='ascii').write(indata). It gives the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range (128).– FergM
Dec 30 '18 at 9:12
Possible duplicate of echo "string" > file in Windows PowerShell appends non-printable character to the file
– TheNavigat
Dec 30 '18 at 9:25
Apparently PowerShell is creating an UTF-16 file. Try to use
encoding='utf-16'. Or change your powershell script to write an actual ASCII/utf-8 file.– Bakuriu
Dec 30 '18 at 9:59