I have some issue putting variable into the path [duplicate]











up vote
-2
down vote

favorite













This question already has an answer here:




  • How do I put a variable inside a String in Python?

    6 answers




it 's possible to put a variable into the path in python/linux



for example :



>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image'counter'.jpeg', 'rb'))


I have syntax error when i do that.










share|improve this question















marked as duplicate by Daniel Roseman, usr2564301, tripleee linux
Users with the  linux badge can single-handedly close linux questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 15:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The documentation might be helpful here
    – DavidG
    Nov 12 at 9:59












  • @Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
    – Adam Mitchell
    Nov 12 at 10:59















up vote
-2
down vote

favorite













This question already has an answer here:




  • How do I put a variable inside a String in Python?

    6 answers




it 's possible to put a variable into the path in python/linux



for example :



>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image'counter'.jpeg', 'rb'))


I have syntax error when i do that.










share|improve this question















marked as duplicate by Daniel Roseman, usr2564301, tripleee linux
Users with the  linux badge can single-handedly close linux questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 15:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The documentation might be helpful here
    – DavidG
    Nov 12 at 9:59












  • @Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
    – Adam Mitchell
    Nov 12 at 10:59













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite












This question already has an answer here:




  • How do I put a variable inside a String in Python?

    6 answers




it 's possible to put a variable into the path in python/linux



for example :



>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image'counter'.jpeg', 'rb'))


I have syntax error when i do that.










share|improve this question
















This question already has an answer here:




  • How do I put a variable inside a String in Python?

    6 answers




it 's possible to put a variable into the path in python/linux



for example :



>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image'counter'.jpeg', 'rb'))


I have syntax error when i do that.





This question already has an answer here:




  • How do I put a variable inside a String in Python?

    6 answers








python linux variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:29









betontalpfa

8351023




8351023










asked Nov 12 at 9:56









Castelo

11




11




marked as duplicate by Daniel Roseman, usr2564301, tripleee linux
Users with the  linux badge can single-handedly close linux questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 15:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Daniel Roseman, usr2564301, tripleee linux
Users with the  linux badge can single-handedly close linux questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 15:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • The documentation might be helpful here
    – DavidG
    Nov 12 at 9:59












  • @Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
    – Adam Mitchell
    Nov 12 at 10:59


















  • The documentation might be helpful here
    – DavidG
    Nov 12 at 9:59












  • @Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
    – Adam Mitchell
    Nov 12 at 10:59
















The documentation might be helpful here
– DavidG
Nov 12 at 9:59






The documentation might be helpful here
– DavidG
Nov 12 at 9:59














@Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
– Adam Mitchell
Nov 12 at 10:59




@Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most.
– Adam Mitchell
Nov 12 at 10:59












3 Answers
3






active

oldest

votes

















up vote
2
down vote













You could use an f-string if you’re working in python 3.6+



This is the most efficient method.



counter = 0
filepath = f"/home/user/image{counter}.jpeg"
image = ClImage(file_obj=open(filepath, 'rb'))


Otherwise the second best would be using the .format() function:



counter = 0
filepath = "/home/user/image{0}.jpeg".format(counter)
image = ClImage(file_obj=open(filepath, 'rb'))





share|improve this answer






























    up vote
    1
    down vote













    You can use Python's .format() method:



    counter = 0
    filepath = '/home/user/image{0}.jpeg'.format(counter)
    image = ClImage(file_obj=open(filepath, 'rb'))





    share|improve this answer




























      up vote
      1
      down vote













      You need string concatenation.



      >>>counter = 0;

      >>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))





      share|improve this answer






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        You could use an f-string if you’re working in python 3.6+



        This is the most efficient method.



        counter = 0
        filepath = f"/home/user/image{counter}.jpeg"
        image = ClImage(file_obj=open(filepath, 'rb'))


        Otherwise the second best would be using the .format() function:



        counter = 0
        filepath = "/home/user/image{0}.jpeg".format(counter)
        image = ClImage(file_obj=open(filepath, 'rb'))





        share|improve this answer



























          up vote
          2
          down vote













          You could use an f-string if you’re working in python 3.6+



          This is the most efficient method.



          counter = 0
          filepath = f"/home/user/image{counter}.jpeg"
          image = ClImage(file_obj=open(filepath, 'rb'))


          Otherwise the second best would be using the .format() function:



          counter = 0
          filepath = "/home/user/image{0}.jpeg".format(counter)
          image = ClImage(file_obj=open(filepath, 'rb'))





          share|improve this answer

























            up vote
            2
            down vote










            up vote
            2
            down vote









            You could use an f-string if you’re working in python 3.6+



            This is the most efficient method.



            counter = 0
            filepath = f"/home/user/image{counter}.jpeg"
            image = ClImage(file_obj=open(filepath, 'rb'))


            Otherwise the second best would be using the .format() function:



            counter = 0
            filepath = "/home/user/image{0}.jpeg".format(counter)
            image = ClImage(file_obj=open(filepath, 'rb'))





            share|improve this answer














            You could use an f-string if you’re working in python 3.6+



            This is the most efficient method.



            counter = 0
            filepath = f"/home/user/image{counter}.jpeg"
            image = ClImage(file_obj=open(filepath, 'rb'))


            Otherwise the second best would be using the .format() function:



            counter = 0
            filepath = "/home/user/image{0}.jpeg".format(counter)
            image = ClImage(file_obj=open(filepath, 'rb'))






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 10:49

























            answered Nov 12 at 10:44









            Jaba

            6,786165292




            6,786165292
























                up vote
                1
                down vote













                You can use Python's .format() method:



                counter = 0
                filepath = '/home/user/image{0}.jpeg'.format(counter)
                image = ClImage(file_obj=open(filepath, 'rb'))





                share|improve this answer

























                  up vote
                  1
                  down vote













                  You can use Python's .format() method:



                  counter = 0
                  filepath = '/home/user/image{0}.jpeg'.format(counter)
                  image = ClImage(file_obj=open(filepath, 'rb'))





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You can use Python's .format() method:



                    counter = 0
                    filepath = '/home/user/image{0}.jpeg'.format(counter)
                    image = ClImage(file_obj=open(filepath, 'rb'))





                    share|improve this answer












                    You can use Python's .format() method:



                    counter = 0
                    filepath = '/home/user/image{0}.jpeg'.format(counter)
                    image = ClImage(file_obj=open(filepath, 'rb'))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 10:02









                    Adam Mitchell

                    7331627




                    7331627






















                        up vote
                        1
                        down vote













                        You need string concatenation.



                        >>>counter = 0;

                        >>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))





                        share|improve this answer



























                          up vote
                          1
                          down vote













                          You need string concatenation.



                          >>>counter = 0;

                          >>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))





                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You need string concatenation.



                            >>>counter = 0;

                            >>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))





                            share|improve this answer














                            You need string concatenation.



                            >>>counter = 0;

                            >>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 12 at 10:06









                            Lie Ryan

                            44.3k968121




                            44.3k968121










                            answered Nov 12 at 10:03









                            betontalpfa

                            8351023




                            8351023















                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python