0

I can successfully read from a file. However, I can't get an operation results to write.

For ex.:

i edited to include the full code with a recommended change

if 5:
 from pathlib import Path
 a = Path(r"C:\Users\l\Desktop\a.txt").read_text()
 a = int(a)
 qqa = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~'
 def x(qqc, qqd):
   qqe = []
   while qqc > 0:
     qqf = qqc % qqd
     qqe.append(qqf)
     qqc = qqc // qqd
   qqg = []
   while qqe:
     qqg.append(qqa[qqe.pop()])
   return ''.join(qqg)
 b = open(r"C:\Users\l\Desktop\b.txt","w")
 b.write(str(x(a,12)))

To explain this:

a.txt contains only the string “24” (excluding the quotes)
x is a base-10 to base-_ conversion operation
“12” is the base

I don't want the write operation to write the literal operation “x(a,12)” (excluding the quotes).

I want the write operation to write the result “20” (excluding the quotes). (“24” in base-12 is “20”.)

How can I get this to work properly in Python 3?

3
  • what error are you getting? Commented May 18, 2019 at 2:01
  • Incidentally, what is is 5? Commented May 18, 2019 at 2:02
  • The actual contents of your x() function would appear to be the single most important detail to your question - yet you have chosen to omit it completely. Or is the actual question how to write that function? Commented May 18, 2019 at 2:06

2 Answers 2

1

As I understand you are getting TypeError: write() argument must be str, not int All you have to do is cast the int to str b.write(str(x(a,12)))

Sign up to request clarification or add additional context in comments.

4 Comments

· i tried this, but it just prints "2" in the console · i edited my post to include your recommendation in my code · also, here's a quick video of me doing the whole process: dropbox.com/s/13v7niqlb67y4k6/python.mp4?dl=0
@abcjme, 2 is the expected result of the .write() call, which returns the number of characters that were written. Have you actually looked at the file, to see if the right two characters were written to it?
@jasonharper edit: nevermind, i had a simple typo · it's working now
@Bikram Shrestha nevermind, i had a simple typo · it's working now · thanks
1

write() argument must be a string, so in a simple way:

b.write(str(x(a,12)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.