Python Forum
write in a distant file, code improvement with a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write in a distant file, code improvement with a list
#1
hello, I have a code, and it is simply, but it need to be improved. My code get all value in the line. They assigned the value to the number of the line. After that, they write in the file, line per line, the value, and one line write the value and add 1 to the value.

This is my code, the numbers is in french, is 1 to 40
import paramiko
import os
from paramiko import sftp_handle
quaranteetun=41
zero=0
un=0
deux=0
trois=0
quatre=0
cinq=0
six=0
sept=0
huit=0
neuf=0
dix=0
onze=0
douze=0
treize=0
quatorze=0
quinze=0
seize=0
dixsept=0
dixhuit=0
dixneuf=0
vingt=0
vingtetun=0
vingtdeux=0
vingttrois=0
vingtquatre=0
vingtcinq=0
vingtsix=0
vingtsept=0
vingthuit=0
vingtneuf=0
trente=0	
trenteetun=0
trentedeux=0
trentetrois=0
trentequatre=0
trentecinq=0
trentesix=0
trentesept=0
trentehuit=0
trenteneuf=0
quarante=0
x="un"
i=1
b=0
variable=41
file="/home/pi/SiteCyberdependance/donne/donne.txt"
self=paramiko.SFTPFile.write
read = paramiko.SFTPFile.read
ssh_client = paramiko.SSHClient
open = paramiko.SFTPClient.open
write=paramiko.SFTPFile._write

host = "41.41.41.41"
port = 22
transport = paramiko.Transport((host, port))
password = "41"
username = "pi"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
SFTPFile=paramiko.SFTPFile.write


f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "r+")
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt") as f:
    for line in f:
        line=int(line)
        b=line
        print(line, end='')
        while x==quarante:
            quarante=b
        while x==trenteneuf:
            trenteneuf=b
            x=quarante
        while x==trentehuit:
            trentehuit=b
            x=trenteneuf
        while x==trentesept:
            trentesept=b
            x="trentehuit"
        while x==trentesix:
            trentesix=b
            x="trentesept"
        while x==trentecinq:
            trentecinq=b
            x=trentesix
        while x==trentequatre:
            trentequatre=b
            x=trentecinq
        while x==trentetrois:
            trentetrois=b
            x=trentequatre
        while x==trentedeux:
            trentedeux=b
            x=trentetrois
        while x==trenteetun:
            trenteetun=b
            x=trentedeux"
        while x==trente:
            trente=b
            x=trenteetun
        while x==vingtneuf:
            vingtneuf=b
            x=trente
        while x==vingthuit:
            vingthuit=b
            x=vingtneuf
        while x==vingtsept:
            vingtsept=b
            x=vingthuit
        while x==vingtsix:
            vingtsix=b
            x=vingtsept
        while x==vingtcinq:
            vingtcinq=b
            x=vingtsix
        while x==vingtquatre:
            vingtquatre=b
            x=vingtcinq
        while x==vingttrois:
            vingttrois=b
            x=vingtquatre
        while x==vingtdeux:
            vingtdeux=b
            x=vingttrois
        while x==vingtetun:
            vingtetun=b
            x=vingtdeux
        while x==vingt:
            vingt=b
            x=vingtetun
        while x==dixneuf:
            dixneuf=b
            x=vingt
        while x==dixhuit:
            dixhuit=b
            x=dixneuf
        while x==dixsept:
            dixsept=b
            x=dixhuit
        while x==seize:
            seize=b
            x=dixsept
        while x==quinze:
            quinze=b
            x=seize
        while x==quatorze:
            quatorze=b
            x=quinze
        while x==treize:
            treize=b
            x=quatorze
        while x==douze:
            douze=b
            x=treize
        while x==onze:
            onze=b
            x=douze
        while x==dix:
            dix=b
            x=onze
        while b==neuf:
            neuf=b
            x=dix
        while x==huit:
            huit=b
            x=neuf
        while x==sept:
            sept=b
            x=huit
        while x==six:
            six=b
            x=sept
        while x==cinq:
            cinq=b
            x=six
        while x==quatre:
            quatre=b
            x=cinq
        while x==trois:
            trois=b
            x=quatre
        while x==deux:
            deux=b
            x=trois
        while x==un:
            un=b
            x=deux
        while x==zero:
            un=b
            un+1
            x=un



f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "w")
x="zero"
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt") as f:
    for x in range(1, 40):
            while x=="trenteneuf":
                b=quarante
                x=("quarante")
            while x=="trentehuit":
                b=trenteneuf
                x=("trenteneuf")
            while x=="trentesept":
                b=trentehuit
                x=("trentehuit")
            while x=="trentesix":
                b=trentesept
                x="trentesept"
            while x=="trentecinq":
                b=trentesix
                x="trentesix"
            while x=="trentequatre":
                b=trentecinq
                x="trentecinq"
            while x=="trentetrois":
                b=trentequatre
                x="trentequatre"
            while x=="trentedeux":
                x="trentetrois"
            while x=="trenteetun":
                b=trentedeux
                x="trentedeux"
            while x=="trente":
                b=trenteetun
                x="trenteetun"
            while x=="vingtneuf":
                b=trente
                x="trente"
            while x=="vingthuit":
                b=vingtneuf
                x="vingtneuf"
            while x=="vingtsept":
                b=vingthuit
                x="vingthuit"
            while x=="vingtsix":
                b=vingtsept
                x="vingtsept"
            while x=="vingtcinq":
                b=vingtsix
                x="vingtsix"
            while x=="vingtquatre":
                b=vingtcinq
                x="vingtcinq"
            while x=="vingttrois":
                b=vingtquatre
                x="vingtquatre"
            while x=="vingtdeux":
                b=vingttrois
                x="vingttrois"
            while x=="vingtetun":
                b=vingtdeux
                x="vingtdeux"
            while x=="vingt":
                b=vingtetun
                x="vingtetun"
            while x=="dixneuf":
                b=vingt
                x="vingt"
            while x=="dixhuit":
                b=dixneuf
                x="dixneuf"
            while x=="dixsept":
                dixsept=b
                x="dixhuit"
            while x=="seize":
                b=dixsept
                x="dixsept"
            while x=="quinze":
                b=seize
                x="seize"
            while x=="quatorze":
                b=quinze
                x="quinze"
            while x=="treize":
                b=quartoze
                x="quatorze"
            while x=="douze":
                b=treize
                x="treize"
            while x=="onze":
                b=douze
                x="douze"
            while x=="dix":
                b=onze
                x="onze"
            while b=="neuf":
                b=dix
                x=str("dix")
            while x=="huit":
                b=neuf
                x=str("neuf")
            while x=="sept":
                b=huit
                x=str("huit")
            while x=="six":
                b=sept
                x=str("sept")
            while x=="cinq":
                b=six
                x=str("six")
            while x=="quatre":
                b=cinq
                x=str("cinq")
            while x=="trois":
                b=quatre
                x=str("quatre")
            while x=="deux":
                b=trois
                x=str("trois")
            while x=="un":
                b=deux
                x=str("deux")
            while x=="zero":
                x="un"
            b=str(int(b))
            sftp=paramiko.SFTPFile.write
            write(self, data=b)
I know I can make it with a list, I have tried, but I got error all the time.
Thank you for you time
Reply
#2
hello, I have a code, and it is simply, but it need to be improved. My code get all value in the line. They assigned the value to the number of the line. After that, they write in the file, line per line, the value, and one line write the value and add 1 to the value.

This is my code, the numbers is in french, is 1 to 40
import paramiko
import os
from paramiko import sftp_handle
quaranteetun=41
zero=0 # number 0 to 40 in french
un=0
deux=0
trois=0
quatre=0
cinq=0
six=0
sept=0
huit=0
neuf=0
dix=0
onze=0
douze=0
treize=0
quatorze=0
quinze=0
seize=0
dixsept=0
dixhuit=0
dixneuf=0
vingt=0
vingtetun=0
vingtdeux=0
vingttrois=0
vingtquatre=0
vingtcinq=0
vingtsix=0
vingtsept=0
vingthuit=0
vingtneuf=0
trente=0	
trenteetun=0
trentedeux=0
trentetrois=0
trentequatre=0
trentecinq=0
trentesix=0
trentesept=0
trentehuit=0
trenteneuf=0
quarante=0 # end of the number 0 to 40 in french
x="un"
i=1
b=0
variable=41
file="/home/pi/SiteCyberdependance/donne/donne.txt"
self=paramiko.SFTPFile.write
read = paramiko.SFTPFile.read
ssh_client = paramiko.SSHClient
open = paramiko.SFTPClient.open
write=paramiko.SFTPFile._write

host = "41.41.41.41"
port = 22
transport = paramiko.Transport((host, port))
password = "41"
username = "pi"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
SFTPFile=paramiko.SFTPFile.write


f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "r+")
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt") as f:
    for line in f:
        line=int(line)
        b=line
        print(line, end='')
        while x==quarante: #quarante correspond to 40, x is equal to the line number, quarante stock the value of the line 40. each 40 line have a value stocked.
            quarante=b #var quarante(40) correspond to b, b correspond to the line 40, this for stock the number in the line
        while x==trenteneuf: #same thing than above, but is 39
            trenteneuf=b
            x=quarante    #this is for switch the line we get the value
        while x==trentehuit:#38
            trentehuit=b
            x=trenteneuf
        while x==trentesept:#37
            trentesept=b
            x=trentehuit
        while x==trentesix:#36
            trentesix=b
            x=trentesept
        while x==trentecinq:#35
            trentecinq=b
            x=trentesix
        while x==trentequatre:#34
            trentequatre=b
            x=trentecinq
        while x==trentetrois:#33
            trentetrois=b
            x=trentequatre
        while x==trentedeux:#32
            trentedeux=b
            x=trentetrois
        while x==trenteetun:#31
            trenteetun=b
            x=trentedeux
        while x==trente:#30
            trente=b
            x=trenteetun
        while x==vingtneuf:#29
            vingtneuf=b
            x=trente
        while x==vingthuit:#28
            vingthuit=b
            x=vingtneuf
        while x==vingtsept:#27
            vingtsept=b
            x=vingthuit
        while x==vingtsix:#26
            vingtsix=b
            x=vingtsept
        while x==vingtcinq:#25
            vingtcinq=b
            x=vingtsix
        while x==vingtquatre:#24
            vingtquatre=b
            x=vingtcinq
        while x==vingttrois:#23
            vingttrois=b
            x=vingtquatre
        while x==vingtdeux:#22
            vingtdeux=b
            x=vingttrois
        while x==vingtetun:#21
            vingtetun=b
            x=vingtdeux
        while x==vingt:#20
            vingt=b
            x=vingtetun
        while x==dixneuf:#19
            dixneuf=b
            x=vingt
        while x==dixhuit:#18
            dixhuit=b
            x=dixneuf
        while x==dixsept:#17
            dixsept=b
            x=dixhuit
        while x==seize:#16
            seize=b
            x=dixsept
        while x==quinze:#15
            quinze=b
            x=seize
        while x==quatorze:#14
            quatorze=b
            x=quinze
        while x==treize:#13
            treize=b
            x=quatorze
        while x==douze:#12
            douze=b
            x=treize
        while x==onze:#11
            onze=b
            x=douze
        while x==dix:#10
            dix=b
            x=onze
        while b==neuf:#9
            neuf=b
            x=dix
        while x==huit:#8
            huit=b
            x=neuf
        while x==sept:#7
            sept=b
            x=huit
        while x==six:#6
            six=b
            x=sept
        while x==cinq:#5
            cinq=b
            x=six
        while x==quatre:#4
            quatre=b
            x=cinq
        while x==trois:#3
            trois=b
            x=quatre
        while x==deux:#2
            deux=b
            x=trois
        while x==un:#1
            un=b
            un+1
            x=deux


# here we write the stocked value, b= value stock in the line and write it, I have tried to put [b]""[/b] around the number, because we want the string "trenteneuf" not the value of trenteneuf
f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "w")
x="zero"
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt") as f:
    for x in range(1, 40):
            while x=="trenteneuf": 
                b=quarante
                x=("quarante")
            while x=="trentehuit":
                b=trenteneuf
                x=("trenteneuf")
            while x=="trentesept":
                b=trentehuit
                x=("trentehuit")
            while x=="trentesix":
                b=trentesept
                x="trentesept"
            while x=="trentecinq":
                b=trentesix
                x="trentesix"
            while x=="trentequatre":
                b=trentecinq
                x="trentecinq"
            while x=="trentetrois":
                b=trentequatre
                x="trentequatre"
            while x=="trentedeux":
                x="trentetrois"
            while x=="trenteetun":
                b=trentedeux
                x="trentedeux"
            while x=="trente":
                b=trenteetun
                x="trenteetun"
            while x=="vingtneuf":
                b=trente
                x="trente"
            while x=="vingthuit":
                b=vingtneuf
                x="vingtneuf"
            while x=="vingtsept":
                b=vingthuit
                x="vingthuit"
            while x=="vingtsix":
                b=vingtsept
                x="vingtsept"
            while x=="vingtcinq":
                b=vingtsix
                x="vingtsix"
            while x=="vingtquatre":
                b=vingtcinq
                x="vingtcinq"
            while x=="vingttrois":
                b=vingtquatre
                x="vingtquatre"
            while x=="vingtdeux":
                b=vingttrois
                x="vingttrois"
            while x=="vingtetun":
                b=vingtdeux
                x="vingtdeux"
            while x=="vingt":
                b=vingtetun
                x="vingtetun"
            while x=="dixneuf":
                b=vingt
                x="vingt"
            while x=="dixhuit":
                b=dixneuf
                x="dixneuf"
            while x=="dixsept":
                dixsept=b
                x="dixhuit"
            while x=="seize":
                b=dixsept
                x="dixsept"
            while x=="quinze":
                b=seize
                x="seize"
            while x=="quatorze":
                b=quinze
                x="quinze"
            while x=="treize":
                b=quartoze
                x="quatorze"
            while x=="douze":
                b=treize
                x="treize"
            while x=="onze":
                b=douze
                x="douze"
            while x=="dix":
                b=onze
                x="onze"
            while b=="neuf":
                b=dix
                x=str("dix")
            while x=="huit":
                b=neuf
                x=str("neuf")
            while x=="sept":
                b=huit
                x=str("huit")
            while x=="six":
                b=sept
                x=str("sept")
            while x=="cinq":
                b=six
                x=str("six")
            while x=="quatre":
                b=cinq
                x=str("cinq")
            while x=="trois":
                b=quatre
                x=str("quatre")
            while x=="deux":
                b=trois
                x=str("trois")
            while x=="un":
                b=deux
                x=str("deux")
            while x=="zero":
                x="un"
            b=str(int(b))
            sftp=paramiko.SFTPFile.write
            write(self, data=b)
I know I can make it with a list, I have tried, but I got error all the time.
Thank you for you time
Reply
#3
My advice would be to use a list. So why don't you show us the code where you tried to use a list, and tell us exactly how it's not working, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
This is my code with a list.
import paramiko
line=0
b=[line]
file="/home/pi/SiteCyberdependance/donne/donne.txt"
self=paramiko.SFTPFile.write
readlines=paramiko.SFTPFile.readlines
ssh_client = paramiko.SSHClient
open = paramiko.SFTPClient.open
write=paramiko.SFTPFile._write

host = "41.41.41.41"
port = 22
transport = paramiko.Transport((host, port))
password = "41"
username = "pi"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
SFTPFile=paramiko.SFTPFile.write


f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "r+")
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt") as f:  
        b=f.read().splitlines()
        
        


print(b)

f = sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "w+")
with sftp.open("/home/pi/SiteCyberdependance/donne/donne.txt", "w") as f:
    for item in b:
            sftp=paramiko.SFTPFile.write
            f.write("%s\n" %item)
That work, but the code print this

b'16'b'1'b'1'b'3'b'6'b'5'b'87'b'4'b'83'b'0'b'2'b'7'b'86'b'54'b'32'b'78'b'45'b'21'b'23'b'41'b'35'b'76'b'90'
I need to print it in different lines in the right order without the b""
Reply
#5
What are the contents of the text file before you read it? If that is the output of the print statement, there are no newline characters in the file, rendering the splitlines call useless (note that you can use f.readlines() instead).

I would note that 21, 30, and 33 don't really do anything, so you should be able to delete them. 21 and 30 are undone by 22 and 31, but note that you have different file specifications on those pairs of lines.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
the content of the file line per line:
16
1
1
3
6
5
87
4
83
0
2
7
86
54
32
78
45
21
23
41
35
76
90
Reply
#7
I don't understand what is going on, then. The smtp package seems to be reading binary strings, but I'm not familiar with that package.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
the program get exactly what I want. but when the program write, he write the variable (here is "b") and the the value between "" and he write all the value in one line.
I need just to print the value
example: the value in the fisrt line is 16, the program have no trouble to do that. but when he write the value, he write the var and the value between "", was result that b"16". the problem is I just need the value, not the var where is stock the value. and other problem was I need to write each value in diferent line, not the same. eventualy, the final problem will be to increment the line, but I know how I will do that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Any suggestions for improvement?(my first python code) PyAlex 9 5,971 Sep-08-2018, 06:45 PM
Last Post: PyAlex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020